2014-10-27 Andrew MacLeod <amacleod@redhat.com>
[official-gcc.git] / gcc / config / i386 / winnt.c
blob288cbde0ed0f9e96843ef8706a6cb7431dafd42f
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 "hash-table.h"
41 #include "vec.h"
42 #include "predict.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "machmode.h"
46 #include "input.h"
47 #include "function.h"
48 #include "dominance.h"
49 #include "cfg.h"
50 #include "cfgrtl.h"
51 #include "cfganal.h"
52 #include "lcm.h"
53 #include "cfgbuild.h"
54 #include "cfgcleanup.h"
55 #include "basic-block.h"
56 #include "tree-ssa-alias.h"
57 #include "internal-fn.h"
58 #include "gimple-fold.h"
59 #include "tree-eh.h"
60 #include "gimple-expr.h"
61 #include "is-a.h"
62 #include "gimple.h"
63 #include "lto-streamer.h"
64 #include "lto-section-names.h"
65 #include "builtins.h"
67 /* i386/PE specific attribute support.
69 i386/PE has two new attributes:
70 dllexport - for exporting a function/variable that will live in a dll
71 dllimport - for importing a function/variable from a dll
73 Microsoft allows multiple declspecs in one __declspec, separating
74 them with spaces. We do NOT support this. Instead, use __declspec
75 multiple times.
78 /* Handle a "shared" attribute;
79 arguments as in struct attribute_spec.handler. */
80 tree
81 ix86_handle_shared_attribute (tree *node, tree name, tree, int,
82 bool *no_add_attrs)
84 if (TREE_CODE (*node) != VAR_DECL)
86 warning (OPT_Wattributes, "%qE attribute only applies to variables",
87 name);
88 *no_add_attrs = true;
91 return NULL_TREE;
94 /* Handle a "selectany" attribute;
95 arguments as in struct attribute_spec.handler. */
96 tree
97 ix86_handle_selectany_attribute (tree *node, tree name, tree, int,
98 bool *no_add_attrs)
100 /* The attribute applies only to objects that are initialized and have
101 external linkage. However, we may not know about initialization
102 until the language frontend has processed the decl. We'll check for
103 initialization later in encode_section_info. */
104 if (TREE_CODE (*node) != VAR_DECL || !TREE_PUBLIC (*node))
106 error ("%qE attribute applies only to initialized variables"
107 " with external linkage", name);
108 *no_add_attrs = true;
111 return NULL_TREE;
115 /* Return the type that we should use to determine if DECL is
116 imported or exported. */
118 static tree
119 associated_type (tree decl)
121 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
122 ? DECL_CONTEXT (decl) : NULL_TREE);
125 /* Return true if DECL should be a dllexport'd object. */
127 static bool
128 i386_pe_determine_dllexport_p (tree decl)
130 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
131 return false;
133 /* Don't export local clones of dllexports. */
134 if (!TREE_PUBLIC (decl))
135 return false;
137 if (TREE_CODE (decl) == FUNCTION_DECL
138 && DECL_DECLARED_INLINE_P (decl)
139 && !flag_keep_inline_dllexport)
140 return false;
142 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
143 return true;
145 return false;
148 /* Return true if DECL should be a dllimport'd object. */
150 static bool
151 i386_pe_determine_dllimport_p (tree decl)
153 tree assoc;
155 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
156 return false;
158 if (DECL_DLLIMPORT_P (decl))
159 return true;
161 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
162 by targetm.cxx.adjust_class_at_definition. Check again to emit
163 error message if the class attribute has been overridden by an
164 out-of-class definition of static data. */
165 assoc = associated_type (decl);
166 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc))
167 && TREE_CODE (decl) == VAR_DECL
168 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
169 && !DECL_EXTERNAL (decl)
170 /* vtable's are linkonce constants, so defining a vtable is not
171 an error as long as we don't try to import it too. */
172 && !DECL_VIRTUAL_P (decl))
173 error ("definition of static data member %q+D of "
174 "dllimport%'d class", decl);
176 return false;
179 /* Handle the -mno-fun-dllimport target switch. */
181 bool
182 i386_pe_valid_dllimport_attribute_p (const_tree decl)
184 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
185 return false;
186 return true;
189 /* Return string which is the function name, identified by ID, modified
190 with a suffix consisting of an atsign (@) followed by the number of
191 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
192 FASTCALL is true, also add the FASTCALL_PREFIX.
193 Return NULL if no change required. */
195 static tree
196 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
198 HOST_WIDE_INT total = 0;
199 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
200 char *new_str, *p;
201 tree type = TREE_TYPE (DECL_ORIGIN (decl));
202 tree arg;
203 function_args_iterator args_iter;
205 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
207 if (prototype_p (type))
209 /* This attribute is ignored for variadic functions. */
210 if (stdarg_p (type))
211 return NULL_TREE;
213 /* Quit if we hit an incomplete type. Error is reported
214 by convert_arguments in c-typeck.c or cp/typeck.c. */
215 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
217 HOST_WIDE_INT parm_size;
218 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
220 if (! COMPLETE_TYPE_P (arg))
221 break;
223 parm_size = int_size_in_bytes (arg);
224 if (parm_size < 0)
225 break;
227 /* Must round up to include padding. This is done the same
228 way as in store_one_arg. */
229 parm_size = ((parm_size + parm_boundary_bytes - 1)
230 / parm_boundary_bytes * parm_boundary_bytes);
231 total += parm_size;
235 /* Assume max of 8 base 10 digits in the suffix. */
236 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
237 if (fastcall)
238 *p++ = FASTCALL_PREFIX;
239 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
241 return get_identifier (new_str);
244 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
245 fastcall function. The original identifier is supplied in ID. */
247 static tree
248 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
250 tree new_id = NULL_TREE;
252 if (TREE_CODE (decl) == FUNCTION_DECL)
254 unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
255 if ((ccvt & IX86_CALLCVT_STDCALL) != 0)
257 if (TARGET_RTD)
258 /* If we are using -mrtd emit undecorated symbol and let linker
259 do the proper resolving. */
260 return NULL_TREE;
261 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
263 else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
264 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
267 return new_id;
270 /* Emit an assembler directive to set symbol for DECL visibility to
271 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
272 As for PE there is no hidden support in gas, we just warn for
273 user-specified visibility attributes. */
275 void
276 i386_pe_assemble_visibility (tree decl, int)
278 if (!decl
279 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
280 return;
281 if (!DECL_ARTIFICIAL (decl))
282 warning (OPT_Wattributes, "visibility attribute not supported "
283 "in this configuration; ignored");
286 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
287 in the language-independent default hook
288 langhooks,c:lhd_set_decl_assembler_name ()
289 and in cp/mangle,c:mangle_decl (). */
290 tree
291 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
293 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
295 return (new_id ? new_id : id);
298 /* This hook behaves the same as varasm.c/assemble_name(), but
299 generates the name into memory rather than outputting it to
300 a file stream. */
302 tree
303 i386_pe_mangle_assembler_name (const char *name)
305 const char *skipped = name + (*name == '*' ? 1 : 0);
306 const char *stripped = targetm.strip_name_encoding (skipped);
307 if (*name != '*' && *user_label_prefix && *stripped != FASTCALL_PREFIX)
308 stripped = ACONCAT ((user_label_prefix, stripped, NULL));
309 return get_identifier (stripped);
312 void
313 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
315 rtx symbol;
316 int flags;
318 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
319 default_encode_section_info (decl, rtl, first);
321 /* Careful not to prod global register variables. */
322 if (!MEM_P (rtl))
323 return;
325 symbol = XEXP (rtl, 0);
326 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
328 switch (TREE_CODE (decl))
330 case FUNCTION_DECL:
331 /* FIXME: Imported stdcall names are not modified by the Ada frontend.
332 Check and decorate the RTL name now. */
333 if (strcmp (lang_hooks.name, "GNU Ada") == 0)
335 tree new_id;
336 tree old_id = DECL_ASSEMBLER_NAME (decl);
337 const char* asm_str = IDENTIFIER_POINTER (old_id);
338 /* Do not change the identifier if a verbatim asmspec
339 or if stdcall suffix already added. */
340 if (!(*asm_str == '*' || strchr (asm_str, '@'))
341 && (new_id = i386_pe_maybe_mangle_decl_assembler_name (decl,
342 old_id)))
343 XSTR (symbol, 0) = IDENTIFIER_POINTER (new_id);
345 break;
347 case VAR_DECL:
348 if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
350 if (DECL_INITIAL (decl)
351 /* If an object is initialized with a ctor, the static
352 initialization and destruction code for it is present in
353 each unit defining the object. The code that calls the
354 ctor is protected by a link-once guard variable, so that
355 the object still has link-once semantics, */
356 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
357 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
358 else
359 error ("%q+D:'selectany' attribute applies only to "
360 "initialized objects", decl);
362 break;
364 default:
365 return;
368 /* Mark the decl so we can tell from the rtl whether the object is
369 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
370 handles dllexport/dllimport override semantics. */
371 flags = (SYMBOL_REF_FLAGS (symbol) &
372 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
373 if (i386_pe_determine_dllexport_p (decl))
374 flags |= SYMBOL_FLAG_DLLEXPORT;
375 else if (i386_pe_determine_dllimport_p (decl))
376 flags |= SYMBOL_FLAG_DLLIMPORT;
378 SYMBOL_REF_FLAGS (symbol) = flags;
382 bool
383 i386_pe_binds_local_p (const_tree exp)
385 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
386 && DECL_DLLIMPORT_P (exp))
387 return false;
389 /* External public symbols, which aren't weakref-s,
390 have local-binding for PE targets. */
391 if (DECL_P (exp)
392 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
393 && TREE_PUBLIC (exp)
394 && DECL_EXTERNAL (exp))
395 return true;
396 return default_binds_local_p_1 (exp, 0);
399 /* Also strip the fastcall prefix and stdcall suffix. */
401 const char *
402 i386_pe_strip_name_encoding_full (const char *str)
404 const char *p;
405 const char *name = default_strip_name_encoding (str);
407 /* Strip leading '@' on fastcall symbols. */
408 if (*name == '@')
409 name++;
411 /* Strip trailing "@n". */
412 p = strchr (name, '@');
413 if (p)
414 return ggc_alloc_string (name, p - name);
416 return name;
419 void
420 i386_pe_unique_section (tree decl, int reloc)
422 int len;
423 const char *name, *prefix;
424 char *string;
426 /* Ignore RELOC, if we are allowed to put relocated
427 const data into read-only section. */
428 if (!flag_writable_rel_rdata)
429 reloc = 0;
430 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
431 name = i386_pe_strip_name_encoding_full (name);
433 /* The object is put in, for example, section .text$foo.
434 The linker will then ultimately place them in .text
435 (everything from the $ on is stripped). Don't put
436 read-only data in .rdata section to avoid a PE linker
437 bug when .rdata$* grouped sections are used in code
438 without a .rdata section. */
439 if (TREE_CODE (decl) == FUNCTION_DECL)
440 prefix = ".text$";
441 else if (decl_readonly_section (decl, reloc))
442 prefix = ".rdata$";
443 else
444 prefix = ".data$";
445 len = strlen (name) + strlen (prefix);
446 string = XALLOCAVEC (char, len + 1);
447 sprintf (string, "%s%s", prefix, name);
449 set_decl_section_name (decl, string);
452 /* Local and global relocs can be placed always into readonly memory for
453 memory for PE-COFF targets. */
455 i386_pe_reloc_rw_mask (void)
457 return 0;
460 /* Select a set of attributes for section NAME based on the properties
461 of DECL and whether or not RELOC indicates that DECL's initializer
462 might contain runtime relocations.
464 We make the section read-only and executable for a function decl,
465 read-only for a const data decl, and writable for a non-const data decl.
467 If the section has already been defined, to not allow it to have
468 different attributes, as (1) this is ambiguous since we're not seeing
469 all the declarations up front and (2) some assemblers (e.g. SVR4)
470 do not recognize section redefinitions. */
471 /* ??? This differs from the "standard" PE implementation in that we
472 handle the SHARED variable attribute. Should this be done for all
473 PE targets? */
475 #define SECTION_PE_SHARED SECTION_MACH_DEP
477 unsigned int
478 i386_pe_section_type_flags (tree decl, const char *, int reloc)
480 unsigned int flags;
482 /* Ignore RELOC, if we are allowed to put relocated
483 const data into read-only section. */
484 if (!flag_writable_rel_rdata)
485 reloc = 0;
487 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
488 flags = SECTION_CODE;
489 else if (decl && decl_readonly_section (decl, reloc))
490 flags = 0;
491 else
493 flags = SECTION_WRITE;
495 if (decl && TREE_CODE (decl) == VAR_DECL
496 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
497 flags |= SECTION_PE_SHARED;
500 if (decl && DECL_P (decl) && DECL_ONE_ONLY (decl))
501 flags |= SECTION_LINKONCE;
503 return flags;
506 void
507 i386_pe_asm_named_section (const char *name, unsigned int flags,
508 tree decl)
510 char flagchars[8], *f = flagchars;
512 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
513 if ((flags & SECTION_EXCLUDE) != 0)
514 *f++ = 'e';
515 #endif
517 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
518 /* readonly data */
520 *f++ ='d'; /* This is necessary for older versions of gas. */
521 *f++ ='r';
523 else
525 if (flags & SECTION_CODE)
526 *f++ = 'x';
527 if (flags & SECTION_WRITE)
528 *f++ = 'w';
529 if (flags & SECTION_PE_SHARED)
530 *f++ = 's';
531 #if !defined (HAVE_GAS_SECTION_EXCLUDE) || HAVE_GAS_SECTION_EXCLUDE == 0
532 /* If attribute "e" isn't supported we mark this section as
533 never-load. */
534 if ((flags & SECTION_EXCLUDE) != 0)
535 *f++ = 'n';
536 #endif
539 /* LTO sections need 1-byte alignment to avoid confusing the
540 zlib decompression algorithm with trailing zero pad bytes. */
541 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
542 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
543 *f++ = '0';
545 *f = '\0';
547 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
549 if (flags & SECTION_LINKONCE)
551 /* Functions may have been compiled at various levels of
552 optimization so we can't use `same_size' here.
553 Instead, have the linker pick one, without warning.
554 If 'selectany' attribute has been specified, MS compiler
555 sets 'discard' characteristic, rather than telling linker
556 to warn of size or content mismatch, so do the same. */
557 bool discard = (flags & SECTION_CODE)
558 || (TREE_CODE (decl) != IDENTIFIER_NODE
559 && lookup_attribute ("selectany",
560 DECL_ATTRIBUTES (decl)));
561 fprintf (asm_out_file, "\t.linkonce %s\n",
562 (discard ? "discard" : "same_size"));
566 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
568 void
569 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
570 const char *name, HOST_WIDE_INT size,
571 HOST_WIDE_INT align)
573 HOST_WIDE_INT rounded;
575 /* Compute as in assemble_noswitch_variable, since we don't have
576 support for aligned common on older binutils. We must also
577 avoid emitting a common symbol of size zero, as this is the
578 overloaded representation that indicates an undefined external
579 symbol in the PE object file format. */
580 rounded = size ? size : 1;
581 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
582 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
583 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
585 i386_pe_maybe_record_exported_symbol (decl, name, 1);
587 fprintf (stream, "\t.comm\t");
588 assemble_name (stream, name);
589 if (use_pe_aligned_common)
590 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
591 size ? size : (HOST_WIDE_INT) 1,
592 exact_log2 (align) - exact_log2 (CHAR_BIT));
593 else
594 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
595 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
598 /* The Microsoft linker requires that every function be marked as
599 DT_FCN. When using gas on cygwin, we must emit appropriate .type
600 directives. */
602 #include "gsyms.h"
604 /* Mark a function appropriately. This should only be called for
605 functions for which we are not emitting COFF debugging information.
606 FILE is the assembler output file, NAME is the name of the
607 function, and PUB is nonzero if the function is globally
608 visible. */
610 void
611 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
613 fprintf (file, "\t.def\t");
614 assemble_name (file, name);
615 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
616 pub ? (int) C_EXT : (int) C_STAT,
617 (int) DT_FCN << N_BTSHFT);
620 /* Keep a list of external functions. */
622 struct GTY(()) extern_list
624 struct extern_list *next;
625 tree decl;
626 const char *name;
629 static GTY(()) struct extern_list *extern_head;
631 /* Assemble an external function reference. We need to keep a list of
632 these, so that we can output the function types at the end of the
633 assembly. We can't output the types now, because we might see a
634 definition of the function later on and emit debugging information
635 for it then. */
637 void
638 i386_pe_record_external_function (tree decl, const char *name)
640 struct extern_list *p;
642 p = ggc_alloc<extern_list> ();
643 p->next = extern_head;
644 p->decl = decl;
645 p->name = name;
646 extern_head = p;
649 /* Keep a list of exported symbols. */
651 struct GTY(()) export_list
653 struct export_list *next;
654 const char *name;
655 int is_data; /* used to type tag exported symbols. */
658 /* Keep a list of stub symbols. */
660 struct GTY(()) stub_list
662 struct stub_list *next;
663 const char *name;
666 static GTY(()) struct export_list *export_head;
668 static GTY(()) struct stub_list *stub_head;
670 /* Assemble an export symbol entry. We need to keep a list of
671 these, so that we can output the export list at the end of the
672 assembly. We used to output these export symbols in each function,
673 but that causes problems with GNU ld when the sections are
674 linkonce. Beware, DECL may be NULL if compile_file() is emitting
675 the LTO marker. */
677 void
678 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
680 rtx symbol;
681 struct export_list *p;
683 if (!decl)
684 return;
686 symbol = XEXP (DECL_RTL (decl), 0);
687 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
688 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
689 return;
691 gcc_assert (TREE_PUBLIC (decl));
693 p = ggc_alloc<export_list> ();
694 p->next = export_head;
695 p->name = name;
696 p->is_data = is_data;
697 export_head = p;
700 void
701 i386_pe_record_stub (const char *name)
703 struct stub_list *p;
705 if (!name || *name == 0)
706 return;
708 p = stub_head;
709 while (p != NULL)
711 if (p->name[0] == *name
712 && !strcmp (p->name, name))
713 return;
714 p = p->next;
717 p = ggc_alloc<stub_list> ();
718 p->next = stub_head;
719 p->name = name;
720 stub_head = p;
724 #ifdef CXX_WRAP_SPEC_LIST
726 /* Hashtable helpers. */
728 struct wrapped_symbol_hasher : typed_noop_remove <char>
730 typedef char value_type;
731 typedef char compare_type;
732 static inline hashval_t hash (const value_type *);
733 static inline bool equal (const value_type *, const compare_type *);
734 static inline void remove (value_type *);
737 inline hashval_t
738 wrapped_symbol_hasher::hash (const value_type *v)
740 return htab_hash_string (v);
743 /* Hash table equality helper function. */
745 inline bool
746 wrapped_symbol_hasher::equal (const value_type *x, const compare_type *y)
748 return !strcmp (x, y);
751 /* Search for a function named TARGET in the list of library wrappers
752 we are using, returning a pointer to it if found or NULL if not.
753 This function might be called on quite a few symbols, and we only
754 have the list of names of wrapped functions available to us as a
755 spec string, so first time round we lazily initialise a hash table
756 to make things quicker. */
758 static const char *
759 i386_find_on_wrapper_list (const char *target)
761 static char first_time = 1;
762 static hash_table<wrapped_symbol_hasher> *wrappers;
764 if (first_time)
766 /* Beware that this is not a complicated parser, it assumes
767 that any sequence of non-whitespace beginning with an
768 underscore is one of the wrapped symbols. For now that's
769 adequate to distinguish symbols from spec substitutions
770 and command-line options. */
771 static char wrapper_list_buffer[] = CXX_WRAP_SPEC_LIST;
772 char *bufptr;
773 /* Breaks up the char array into separated strings
774 strings and enter them into the hash table. */
775 wrappers = new hash_table<wrapped_symbol_hasher> (8);
776 for (bufptr = wrapper_list_buffer; *bufptr; ++bufptr)
778 char *found = NULL;
779 if (ISSPACE (*bufptr))
780 continue;
781 if (*bufptr == '_')
782 found = bufptr;
783 while (*bufptr && !ISSPACE (*bufptr))
784 ++bufptr;
785 if (*bufptr)
786 *bufptr = 0;
787 if (found)
788 *wrappers->find_slot (found, INSERT) = found;
790 first_time = 0;
793 return wrappers->find (target);
796 #endif /* CXX_WRAP_SPEC_LIST */
798 /* This is called at the end of assembly. For each external function
799 which has not been defined, we output a declaration now. We also
800 output the .drectve section. */
802 void
803 i386_pe_file_end (void)
805 struct extern_list *p;
807 for (p = extern_head; p != NULL; p = p->next)
809 tree decl;
811 decl = p->decl;
813 /* Positively ensure only one declaration for any given symbol. */
814 if (! TREE_ASM_WRITTEN (decl)
815 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
817 #ifdef CXX_WRAP_SPEC_LIST
818 /* To ensure the DLL that provides the corresponding real
819 functions is still loaded at runtime, we must reference
820 the real function so that an (unused) import is created. */
821 const char *realsym = i386_find_on_wrapper_list (p->name);
822 if (realsym)
823 i386_pe_declare_function_type (asm_out_file,
824 concat ("__real_", realsym, NULL), TREE_PUBLIC (decl));
825 #endif /* CXX_WRAP_SPEC_LIST */
826 TREE_ASM_WRITTEN (decl) = 1;
827 i386_pe_declare_function_type (asm_out_file, p->name,
828 TREE_PUBLIC (decl));
832 if (export_head)
834 struct export_list *q;
835 drectve_section ();
836 for (q = export_head; q != NULL; q = q->next)
838 fprintf (asm_out_file, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
839 default_strip_name_encoding (q->name),
840 (q->is_data ? ",data" : ""));
844 if (stub_head)
846 struct stub_list *q;
848 for (q = stub_head; q != NULL; q = q->next)
850 const char *name = q->name;
851 const char *oname;
853 if (name[0] == '*')
854 ++name;
855 oname = name;
856 if (name[0] == '.')
857 ++name;
858 if (strncmp (name, "refptr.", 7) != 0)
859 continue;
860 name += 7;
861 fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n"
862 "\t.globl\t%s\n"
863 "\t.linkonce\tdiscard\n", oname, oname);
864 fprintf (asm_out_file, "%s:\n\t.quad\t%s\n", oname, name);
870 /* x64 Structured Exception Handling unwind info. */
872 struct seh_frame_state
874 /* SEH records saves relative to the "current" stack pointer, whether
875 or not there's a frame pointer in place. This tracks the current
876 stack pointer offset from the CFA. */
877 HOST_WIDE_INT sp_offset;
879 /* The CFA is located at CFA_REG + CFA_OFFSET. */
880 HOST_WIDE_INT cfa_offset;
881 rtx cfa_reg;
884 /* Set up data structures beginning output for SEH. */
886 void
887 i386_pe_seh_init (FILE *f)
889 struct seh_frame_state *seh;
891 if (!TARGET_SEH)
892 return;
893 if (cfun->is_thunk)
894 return;
896 /* We cannot support DRAP with SEH. We turned off support for it by
897 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
898 gcc_assert (!stack_realign_drap);
900 seh = XCNEW (struct seh_frame_state);
901 cfun->machine->seh = seh;
903 seh->sp_offset = INCOMING_FRAME_SP_OFFSET;
904 seh->cfa_offset = INCOMING_FRAME_SP_OFFSET;
905 seh->cfa_reg = stack_pointer_rtx;
907 fputs ("\t.seh_proc\t", f);
908 assemble_name (f, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun->decl)));
909 fputc ('\n', f);
912 void
913 i386_pe_seh_end_prologue (FILE *f)
915 struct seh_frame_state *seh;
917 if (!TARGET_SEH)
918 return;
919 if (cfun->is_thunk)
920 return;
921 seh = cfun->machine->seh;
923 XDELETE (seh);
924 cfun->machine->seh = NULL;
926 fputs ("\t.seh_endprologue\n", f);
929 static void
930 i386_pe_seh_fini (FILE *f)
932 if (!TARGET_SEH)
933 return;
934 if (cfun->is_thunk)
935 return;
936 fputs ("\t.seh_endproc\n", f);
939 /* Emit an assembler directive to save REG via a PUSH. */
941 static void
942 seh_emit_push (FILE *f, struct seh_frame_state *seh, rtx reg)
944 unsigned int regno = REGNO (reg);
946 gcc_checking_assert (GENERAL_REGNO_P (regno));
948 seh->sp_offset += UNITS_PER_WORD;
949 if (seh->cfa_reg == stack_pointer_rtx)
950 seh->cfa_offset += UNITS_PER_WORD;
952 fputs ("\t.seh_pushreg\t", f);
953 print_reg (reg, 0, f);
954 fputc ('\n', f);
957 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
959 static void
960 seh_emit_save (FILE *f, struct seh_frame_state *seh,
961 rtx reg, HOST_WIDE_INT cfa_offset)
963 unsigned int regno = REGNO (reg);
964 HOST_WIDE_INT offset;
966 /* Negative save offsets are of course not supported, since that
967 would be a store below the stack pointer and thus clobberable. */
968 gcc_assert (seh->sp_offset >= cfa_offset);
969 offset = seh->sp_offset - cfa_offset;
971 fputs ((SSE_REGNO_P (regno) ? "\t.seh_savexmm\t"
972 : GENERAL_REGNO_P (regno) ? "\t.seh_savereg\t"
973 : (gcc_unreachable (), "")), f);
974 print_reg (reg, 0, f);
975 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
978 /* Emit an assembler directive to adjust RSP by OFFSET. */
980 static void
981 seh_emit_stackalloc (FILE *f, struct seh_frame_state *seh,
982 HOST_WIDE_INT offset)
984 /* We're only concerned with prologue stack allocations, which all
985 are subtractions from the stack pointer. */
986 gcc_assert (offset < 0);
987 offset = -offset;
989 if (seh->cfa_reg == stack_pointer_rtx)
990 seh->cfa_offset += offset;
991 seh->sp_offset += offset;
993 /* Do not output the stackalloc in that case (it won't work as there is no
994 encoding for very large frame size). */
995 if (offset < SEH_MAX_FRAME_SIZE)
996 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
999 /* Process REG_CFA_ADJUST_CFA for SEH. */
1001 static void
1002 seh_cfa_adjust_cfa (FILE *f, struct seh_frame_state *seh, rtx pat)
1004 rtx dest, src;
1005 HOST_WIDE_INT reg_offset = 0;
1006 unsigned int dest_regno;
1008 dest = SET_DEST (pat);
1009 src = SET_SRC (pat);
1011 if (GET_CODE (src) == PLUS)
1013 reg_offset = INTVAL (XEXP (src, 1));
1014 src = XEXP (src, 0);
1016 else if (GET_CODE (src) == MINUS)
1018 reg_offset = -INTVAL (XEXP (src, 1));
1019 src = XEXP (src, 0);
1021 gcc_assert (src == stack_pointer_rtx);
1022 gcc_assert (seh->cfa_reg == stack_pointer_rtx);
1023 dest_regno = REGNO (dest);
1025 if (dest_regno == STACK_POINTER_REGNUM)
1026 seh_emit_stackalloc (f, seh, reg_offset);
1027 else if (dest_regno == HARD_FRAME_POINTER_REGNUM)
1029 HOST_WIDE_INT offset;
1031 seh->cfa_reg = dest;
1032 seh->cfa_offset -= reg_offset;
1034 offset = seh->sp_offset - seh->cfa_offset;
1036 gcc_assert ((offset & 15) == 0);
1037 gcc_assert (IN_RANGE (offset, 0, 240));
1039 fputs ("\t.seh_setframe\t", f);
1040 print_reg (seh->cfa_reg, 0, f);
1041 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
1043 else
1044 gcc_unreachable ();
1047 /* Process REG_CFA_OFFSET for SEH. */
1049 static void
1050 seh_cfa_offset (FILE *f, struct seh_frame_state *seh, rtx pat)
1052 rtx dest, src;
1053 HOST_WIDE_INT reg_offset;
1055 dest = SET_DEST (pat);
1056 src = SET_SRC (pat);
1058 gcc_assert (MEM_P (dest));
1059 dest = XEXP (dest, 0);
1060 if (REG_P (dest))
1061 reg_offset = 0;
1062 else
1064 gcc_assert (GET_CODE (dest) == PLUS);
1065 reg_offset = INTVAL (XEXP (dest, 1));
1066 dest = XEXP (dest, 0);
1068 gcc_assert (dest == seh->cfa_reg);
1070 seh_emit_save (f, seh, src, seh->cfa_offset - reg_offset);
1073 /* Process a FRAME_RELATED_EXPR for SEH. */
1075 static void
1076 seh_frame_related_expr (FILE *f, struct seh_frame_state *seh, rtx pat)
1078 rtx dest, src;
1079 HOST_WIDE_INT addend;
1081 /* See the full loop in dwarf2out_frame_debug_expr. */
1082 if (GET_CODE (pat) == PARALLEL || GET_CODE (pat) == SEQUENCE)
1084 int i, n = XVECLEN (pat, 0), pass, npass;
1086 npass = (GET_CODE (pat) == PARALLEL ? 2 : 1);
1087 for (pass = 0; pass < npass; ++pass)
1088 for (i = 0; i < n; ++i)
1090 rtx ele = XVECEXP (pat, 0, i);
1092 if (GET_CODE (ele) != SET)
1093 continue;
1094 dest = SET_DEST (ele);
1096 /* Process each member of the PARALLEL independently. The first
1097 member is always processed; others only if they are marked. */
1098 if (i == 0 || RTX_FRAME_RELATED_P (ele))
1100 /* Evaluate all register saves in the first pass and all
1101 register updates in the second pass. */
1102 if ((MEM_P (dest) ^ pass) || npass == 1)
1103 seh_frame_related_expr (f, seh, ele);
1106 return;
1109 dest = SET_DEST (pat);
1110 src = SET_SRC (pat);
1112 switch (GET_CODE (dest))
1114 case REG:
1115 switch (GET_CODE (src))
1117 case REG:
1118 /* REG = REG: This should be establishing a frame pointer. */
1119 gcc_assert (src == stack_pointer_rtx);
1120 gcc_assert (dest == hard_frame_pointer_rtx);
1121 seh_cfa_adjust_cfa (f, seh, pat);
1122 break;
1124 case PLUS:
1125 addend = INTVAL (XEXP (src, 1));
1126 src = XEXP (src, 0);
1127 if (dest == hard_frame_pointer_rtx)
1128 seh_cfa_adjust_cfa (f, seh, pat);
1129 else if (dest == stack_pointer_rtx)
1131 gcc_assert (src == stack_pointer_rtx);
1132 seh_emit_stackalloc (f, seh, addend);
1134 else
1135 gcc_unreachable ();
1136 break;
1138 default:
1139 gcc_unreachable ();
1141 break;
1143 case MEM:
1144 /* A save of some kind. */
1145 dest = XEXP (dest, 0);
1146 if (GET_CODE (dest) == PRE_DEC)
1148 gcc_checking_assert (GET_MODE (src) == Pmode);
1149 gcc_checking_assert (REG_P (src));
1150 seh_emit_push (f, seh, src);
1152 else
1153 seh_cfa_offset (f, seh, pat);
1154 break;
1156 default:
1157 gcc_unreachable ();
1161 /* This function looks at a single insn and emits any SEH directives
1162 required for unwind of this insn. */
1164 void
1165 i386_pe_seh_unwind_emit (FILE *asm_out_file, rtx_insn *insn)
1167 rtx note, pat;
1168 bool handled_one = false;
1169 struct seh_frame_state *seh;
1171 if (!TARGET_SEH)
1172 return;
1174 /* We free the SEH data once done with the prologue. Ignore those
1175 RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1176 seh = cfun->machine->seh;
1177 if (seh == NULL)
1178 return;
1180 if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
1181 return;
1183 for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
1185 switch (REG_NOTE_KIND (note))
1187 case REG_FRAME_RELATED_EXPR:
1188 pat = XEXP (note, 0);
1189 goto found;
1191 case REG_CFA_DEF_CFA:
1192 case REG_CFA_EXPRESSION:
1193 /* Only emitted with DRAP, which we disable. */
1194 gcc_unreachable ();
1195 break;
1197 case REG_CFA_REGISTER:
1198 /* Only emitted in epilogues, which we skip. */
1199 gcc_unreachable ();
1201 case REG_CFA_ADJUST_CFA:
1202 pat = XEXP (note, 0);
1203 if (pat == NULL)
1205 pat = PATTERN (insn);
1206 if (GET_CODE (pat) == PARALLEL)
1207 pat = XVECEXP (pat, 0, 0);
1209 seh_cfa_adjust_cfa (asm_out_file, seh, pat);
1210 handled_one = true;
1211 break;
1213 case REG_CFA_OFFSET:
1214 pat = XEXP (note, 0);
1215 if (pat == NULL)
1216 pat = single_set (insn);
1217 seh_cfa_offset (asm_out_file, seh, pat);
1218 handled_one = true;
1219 break;
1221 default:
1222 break;
1225 if (handled_one)
1226 return;
1227 pat = PATTERN (insn);
1228 found:
1229 seh_frame_related_expr (asm_out_file, seh, pat);
1232 void
1233 i386_pe_seh_emit_except_personality (rtx personality)
1235 int flags = 0;
1237 if (!TARGET_SEH)
1238 return;
1240 fputs ("\t.seh_handler\t", asm_out_file);
1241 output_addr_const (asm_out_file, personality);
1243 #if 0
1244 /* ??? The current implementation of _GCC_specific_handler requires
1245 both except and unwind handling, regardless of which sorts the
1246 user-level function requires. */
1247 eh_region r;
1248 FOR_ALL_EH_REGION(r)
1250 if (r->type == ERT_CLEANUP)
1251 flags |= 1;
1252 else
1253 flags |= 2;
1255 #else
1256 flags = 3;
1257 #endif
1259 if (flags & 1)
1260 fputs (", @unwind", asm_out_file);
1261 if (flags & 2)
1262 fputs (", @except", asm_out_file);
1263 fputc ('\n', asm_out_file);
1266 void
1267 i386_pe_seh_init_sections (void)
1269 if (TARGET_SEH)
1270 exception_section = get_unnamed_section (0, output_section_asm_op,
1271 "\t.seh_handlerdata");
1274 void
1275 i386_pe_start_function (FILE *f, const char *name, tree decl)
1277 i386_pe_maybe_record_exported_symbol (decl, name, 0);
1278 if (write_symbols != SDB_DEBUG)
1279 i386_pe_declare_function_type (f, name, TREE_PUBLIC (decl));
1280 /* In case section was altered by debugging output. */
1281 if (decl != NULL_TREE)
1282 switch_to_section (function_section (decl));
1283 ASM_OUTPUT_FUNCTION_LABEL (f, name, decl);
1286 void
1287 i386_pe_end_function (FILE *f, const char *, tree)
1289 i386_pe_seh_fini (f);
1293 #include "gt-winnt.h"