* config/i386/i386.c: Use HOST_WIDE_INT_1 instead of (HOST_WIDE_INT) 1
[official-gcc.git] / gcc / config / i386 / winnt.c
blobe698cd5df9839b7201905d1803d6b46a8d6a9379
1 /* Subroutines for insn-output.c for Windows NT.
2 Contributed by Douglas Rupp (drupp@cs.washington.edu)
3 Copyright (C) 1995-2015 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 "hash-set.h"
30 #include "machmode.h"
31 #include "vec.h"
32 #include "double-int.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "symtab.h"
36 #include "wide-int.h"
37 #include "inchash.h"
38 #include "tree.h"
39 #include "fold-const.h"
40 #include "stringpool.h"
41 #include "varasm.h"
42 #include "flags.h"
43 #include "tm_p.h"
44 #include "diagnostic-core.h"
45 #include "hash-table.h"
46 #include "langhooks.h"
47 #include "ggc.h"
48 #include "target.h"
49 #include "except.h"
50 #include "hash-table.h"
51 #include "vec.h"
52 #include "predict.h"
53 #include "input.h"
54 #include "function.h"
55 #include "dominance.h"
56 #include "cfg.h"
57 #include "cfgrtl.h"
58 #include "cfganal.h"
59 #include "lcm.h"
60 #include "cfgbuild.h"
61 #include "cfgcleanup.h"
62 #include "basic-block.h"
63 #include "tree-ssa-alias.h"
64 #include "internal-fn.h"
65 #include "gimple-fold.h"
66 #include "tree-eh.h"
67 #include "gimple-expr.h"
68 #include "is-a.h"
69 #include "gimple.h"
70 #include "hash-map.h"
71 #include "plugin-api.h"
72 #include "ipa-ref.h"
73 #include "cgraph.h"
74 #include "lto-streamer.h"
75 #include "lto-section-names.h"
76 #include "builtins.h"
78 /* i386/PE specific attribute support.
80 i386/PE has two new attributes:
81 dllexport - for exporting a function/variable that will live in a dll
82 dllimport - for importing a function/variable from a dll
84 Microsoft allows multiple declspecs in one __declspec, separating
85 them with spaces. We do NOT support this. Instead, use __declspec
86 multiple times.
89 /* Handle a "shared" attribute;
90 arguments as in struct attribute_spec.handler. */
91 tree
92 ix86_handle_shared_attribute (tree *node, tree name, tree, int,
93 bool *no_add_attrs)
95 if (TREE_CODE (*node) != VAR_DECL)
97 warning (OPT_Wattributes, "%qE attribute only applies to variables",
98 name);
99 *no_add_attrs = true;
102 return NULL_TREE;
105 /* Handle a "selectany" attribute;
106 arguments as in struct attribute_spec.handler. */
107 tree
108 ix86_handle_selectany_attribute (tree *node, tree name, tree, int,
109 bool *no_add_attrs)
111 /* The attribute applies only to objects that are initialized and have
112 external linkage. However, we may not know about initialization
113 until the language frontend has processed the decl. We'll check for
114 initialization later in encode_section_info. */
115 if (TREE_CODE (*node) != VAR_DECL || !TREE_PUBLIC (*node))
117 error ("%qE attribute applies only to initialized variables"
118 " with external linkage", name);
119 *no_add_attrs = true;
122 return NULL_TREE;
126 /* Return the type that we should use to determine if DECL is
127 imported or exported. */
129 static tree
130 associated_type (tree decl)
132 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
133 ? DECL_CONTEXT (decl) : NULL_TREE);
136 /* Return true if DECL should be a dllexport'd object. */
138 static bool
139 i386_pe_determine_dllexport_p (tree decl)
141 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
142 return false;
144 /* Don't export local clones of dllexports. */
145 if (!TREE_PUBLIC (decl))
146 return false;
148 if (TREE_CODE (decl) == FUNCTION_DECL
149 && DECL_DECLARED_INLINE_P (decl)
150 && !flag_keep_inline_dllexport)
151 return false;
153 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
154 return true;
156 return false;
159 /* Return true if DECL should be a dllimport'd object. */
161 static bool
162 i386_pe_determine_dllimport_p (tree decl)
164 tree assoc;
166 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
167 return false;
169 if (DECL_DLLIMPORT_P (decl))
170 return true;
172 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
173 by targetm.cxx.adjust_class_at_definition. Check again to emit
174 error message if the class attribute has been overridden by an
175 out-of-class definition of static data. */
176 assoc = associated_type (decl);
177 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc))
178 && TREE_CODE (decl) == VAR_DECL
179 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
180 && !DECL_EXTERNAL (decl)
181 /* vtable's are linkonce constants, so defining a vtable is not
182 an error as long as we don't try to import it too. */
183 && !DECL_VIRTUAL_P (decl))
184 error ("definition of static data member %q+D of "
185 "dllimport%'d class", decl);
187 return false;
190 /* Handle the -mno-fun-dllimport target switch. */
192 bool
193 i386_pe_valid_dllimport_attribute_p (const_tree decl)
195 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
196 return false;
197 return true;
200 /* Return string which is the function name, identified by ID, modified
201 with a suffix consisting of an atsign (@) followed by the number of
202 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
203 FASTCALL is true, also add the FASTCALL_PREFIX.
204 Return NULL if no change required. */
206 static tree
207 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
209 HOST_WIDE_INT total = 0;
210 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
211 char *new_str, *p;
212 tree type = TREE_TYPE (DECL_ORIGIN (decl));
213 tree arg;
214 function_args_iterator args_iter;
216 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
218 if (prototype_p (type))
220 /* This attribute is ignored for variadic functions. */
221 if (stdarg_p (type))
222 return NULL_TREE;
224 /* Quit if we hit an incomplete type. Error is reported
225 by convert_arguments in c-typeck.c or cp/typeck.c. */
226 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
228 HOST_WIDE_INT parm_size;
229 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
231 if (! COMPLETE_TYPE_P (arg))
232 break;
234 parm_size = int_size_in_bytes (arg);
235 if (parm_size < 0)
236 break;
238 /* Must round up to include padding. This is done the same
239 way as in store_one_arg. */
240 parm_size = ((parm_size + parm_boundary_bytes - 1)
241 / parm_boundary_bytes * parm_boundary_bytes);
242 total += parm_size;
246 /* Assume max of 8 base 10 digits in the suffix. */
247 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
248 if (fastcall)
249 *p++ = FASTCALL_PREFIX;
250 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
252 return get_identifier (new_str);
255 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
256 fastcall function. The original identifier is supplied in ID. */
258 static tree
259 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
261 tree new_id = NULL_TREE;
263 if (TREE_CODE (decl) == FUNCTION_DECL)
265 unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
266 if ((ccvt & IX86_CALLCVT_STDCALL) != 0)
268 if (TARGET_RTD)
269 /* If we are using -mrtd emit undecorated symbol and let linker
270 do the proper resolving. */
271 return NULL_TREE;
272 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
274 else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
275 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
278 return new_id;
281 /* Emit an assembler directive to set symbol for DECL visibility to
282 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
283 As for PE there is no hidden support in gas, we just warn for
284 user-specified visibility attributes. */
286 void
287 i386_pe_assemble_visibility (tree decl, int)
289 if (!decl
290 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
291 return;
292 if (!DECL_ARTIFICIAL (decl))
293 warning (OPT_Wattributes, "visibility attribute not supported "
294 "in this configuration; ignored");
297 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
298 in the language-independent default hook
299 langhooks,c:lhd_set_decl_assembler_name ()
300 and in cp/mangle,c:mangle_decl (). */
301 tree
302 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
304 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
306 return (new_id ? new_id : id);
309 /* This hook behaves the same as varasm.c/assemble_name(), but
310 generates the name into memory rather than outputting it to
311 a file stream. */
313 tree
314 i386_pe_mangle_assembler_name (const char *name)
316 const char *skipped = name + (*name == '*' ? 1 : 0);
317 const char *stripped = targetm.strip_name_encoding (skipped);
318 if (*name != '*' && *user_label_prefix && *stripped != FASTCALL_PREFIX)
319 stripped = ACONCAT ((user_label_prefix, stripped, NULL));
320 return get_identifier (stripped);
323 void
324 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
326 rtx symbol;
327 int flags;
329 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
330 default_encode_section_info (decl, rtl, first);
332 /* Careful not to prod global register variables. */
333 if (!MEM_P (rtl))
334 return;
336 symbol = XEXP (rtl, 0);
337 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
339 switch (TREE_CODE (decl))
341 case FUNCTION_DECL:
342 /* FIXME: Imported stdcall names are not modified by the Ada frontend.
343 Check and decorate the RTL name now. */
344 if (strcmp (lang_hooks.name, "GNU Ada") == 0)
346 tree new_id;
347 tree old_id = DECL_ASSEMBLER_NAME (decl);
348 const char* asm_str = IDENTIFIER_POINTER (old_id);
349 /* Do not change the identifier if a verbatim asmspec
350 or if stdcall suffix already added. */
351 if (!(*asm_str == '*' || strchr (asm_str, '@'))
352 && (new_id = i386_pe_maybe_mangle_decl_assembler_name (decl,
353 old_id)))
354 XSTR (symbol, 0) = IDENTIFIER_POINTER (new_id);
356 break;
358 case VAR_DECL:
359 if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
361 if (DECL_INITIAL (decl)
362 /* If an object is initialized with a ctor, the static
363 initialization and destruction code for it is present in
364 each unit defining the object. The code that calls the
365 ctor is protected by a link-once guard variable, so that
366 the object still has link-once semantics, */
367 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
368 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
369 else
370 error ("%q+D:'selectany' attribute applies only to "
371 "initialized objects", decl);
373 break;
375 default:
376 return;
379 /* Mark the decl so we can tell from the rtl whether the object is
380 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
381 handles dllexport/dllimport override semantics. */
382 flags = (SYMBOL_REF_FLAGS (symbol) &
383 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
384 if (i386_pe_determine_dllexport_p (decl))
385 flags |= SYMBOL_FLAG_DLLEXPORT;
386 else if (i386_pe_determine_dllimport_p (decl))
387 flags |= SYMBOL_FLAG_DLLIMPORT;
389 SYMBOL_REF_FLAGS (symbol) = flags;
393 bool
394 i386_pe_binds_local_p (const_tree exp)
396 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
397 && DECL_DLLIMPORT_P (exp))
398 return false;
400 /* External public symbols, which aren't weakref-s,
401 have local-binding for PE targets. */
402 if (DECL_P (exp)
403 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
404 && TREE_PUBLIC (exp)
405 && DECL_EXTERNAL (exp))
406 return true;
407 return default_binds_local_p_1 (exp, 0);
410 /* Also strip the fastcall prefix and stdcall suffix. */
412 const char *
413 i386_pe_strip_name_encoding_full (const char *str)
415 const char *p;
416 const char *name = default_strip_name_encoding (str);
418 /* Strip leading '@' on fastcall symbols. */
419 if (*name == '@')
420 name++;
422 /* Strip trailing "@n". */
423 p = strchr (name, '@');
424 if (p)
425 return ggc_alloc_string (name, p - name);
427 return name;
430 void
431 i386_pe_unique_section (tree decl, int reloc)
433 int len;
434 const char *name, *prefix;
435 char *string;
437 /* Ignore RELOC, if we are allowed to put relocated
438 const data into read-only section. */
439 if (!flag_writable_rel_rdata)
440 reloc = 0;
441 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
442 name = i386_pe_strip_name_encoding_full (name);
444 /* The object is put in, for example, section .text$foo.
445 The linker will then ultimately place them in .text
446 (everything from the $ on is stripped). Don't put
447 read-only data in .rdata section to avoid a PE linker
448 bug when .rdata$* grouped sections are used in code
449 without a .rdata section. */
450 if (TREE_CODE (decl) == FUNCTION_DECL)
451 prefix = ".text$";
452 else if (decl_readonly_section (decl, reloc))
453 prefix = ".rdata$";
454 else
455 prefix = ".data$";
456 len = strlen (name) + strlen (prefix);
457 string = XALLOCAVEC (char, len + 1);
458 sprintf (string, "%s%s", prefix, name);
460 set_decl_section_name (decl, string);
463 /* Local and global relocs can be placed always into readonly memory for
464 memory for PE-COFF targets. */
466 i386_pe_reloc_rw_mask (void)
468 return 0;
471 /* Select a set of attributes for section NAME based on the properties
472 of DECL and whether or not RELOC indicates that DECL's initializer
473 might contain runtime relocations.
475 We make the section read-only and executable for a function decl,
476 read-only for a const data decl, and writable for a non-const data decl.
478 If the section has already been defined, to not allow it to have
479 different attributes, as (1) this is ambiguous since we're not seeing
480 all the declarations up front and (2) some assemblers (e.g. SVR4)
481 do not recognize section redefinitions. */
482 /* ??? This differs from the "standard" PE implementation in that we
483 handle the SHARED variable attribute. Should this be done for all
484 PE targets? */
486 #define SECTION_PE_SHARED SECTION_MACH_DEP
488 unsigned int
489 i386_pe_section_type_flags (tree decl, const char *, int reloc)
491 unsigned int flags;
493 /* Ignore RELOC, if we are allowed to put relocated
494 const data into read-only section. */
495 if (!flag_writable_rel_rdata)
496 reloc = 0;
498 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
499 flags = SECTION_CODE;
500 else if (decl && decl_readonly_section (decl, reloc))
501 flags = 0;
502 else
504 flags = SECTION_WRITE;
506 if (decl && TREE_CODE (decl) == VAR_DECL
507 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
508 flags |= SECTION_PE_SHARED;
511 if (decl && DECL_P (decl) && DECL_ONE_ONLY (decl))
512 flags |= SECTION_LINKONCE;
514 return flags;
517 void
518 i386_pe_asm_named_section (const char *name, unsigned int flags,
519 tree decl)
521 char flagchars[8], *f = flagchars;
523 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
524 if ((flags & SECTION_EXCLUDE) != 0)
525 *f++ = 'e';
526 #endif
528 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
529 /* readonly data */
531 *f++ ='d'; /* This is necessary for older versions of gas. */
532 *f++ ='r';
534 else
536 if (flags & SECTION_CODE)
537 *f++ = 'x';
538 if (flags & SECTION_WRITE)
539 *f++ = 'w';
540 if (flags & SECTION_PE_SHARED)
541 *f++ = 's';
542 #if !defined (HAVE_GAS_SECTION_EXCLUDE) || HAVE_GAS_SECTION_EXCLUDE == 0
543 /* If attribute "e" isn't supported we mark this section as
544 never-load. */
545 if ((flags & SECTION_EXCLUDE) != 0)
546 *f++ = 'n';
547 #endif
550 /* LTO sections need 1-byte alignment to avoid confusing the
551 zlib decompression algorithm with trailing zero pad bytes. */
552 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
553 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
554 *f++ = '0';
556 *f = '\0';
558 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
560 if (flags & SECTION_LINKONCE)
562 /* Functions may have been compiled at various levels of
563 optimization so we can't use `same_size' here.
564 Instead, have the linker pick one, without warning.
565 If 'selectany' attribute has been specified, MS compiler
566 sets 'discard' characteristic, rather than telling linker
567 to warn of size or content mismatch, so do the same. */
568 bool discard = (flags & SECTION_CODE)
569 || (TREE_CODE (decl) != IDENTIFIER_NODE
570 && lookup_attribute ("selectany",
571 DECL_ATTRIBUTES (decl)));
572 fprintf (asm_out_file, "\t.linkonce %s\n",
573 (discard ? "discard" : "same_size"));
577 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
579 void
580 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
581 const char *name, HOST_WIDE_INT size,
582 HOST_WIDE_INT align)
584 HOST_WIDE_INT rounded;
586 /* Compute as in assemble_noswitch_variable, since we don't have
587 support for aligned common on older binutils. We must also
588 avoid emitting a common symbol of size zero, as this is the
589 overloaded representation that indicates an undefined external
590 symbol in the PE object file format. */
591 rounded = size ? size : 1;
592 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
593 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
594 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
596 i386_pe_maybe_record_exported_symbol (decl, name, 1);
598 fprintf (stream, "\t.comm\t");
599 assemble_name (stream, name);
600 if (use_pe_aligned_common)
601 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
602 size ? size : HOST_WIDE_INT_1,
603 exact_log2 (align) - exact_log2 (CHAR_BIT));
604 else
605 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
606 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
609 /* The Microsoft linker requires that every function be marked as
610 DT_FCN. When using gas on cygwin, we must emit appropriate .type
611 directives. */
613 #include "gsyms.h"
615 /* Mark a function appropriately. This should only be called for
616 functions for which we are not emitting COFF debugging information.
617 FILE is the assembler output file, NAME is the name of the
618 function, and PUB is nonzero if the function is globally
619 visible. */
621 void
622 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
624 fprintf (file, "\t.def\t");
625 assemble_name (file, name);
626 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
627 pub ? (int) C_EXT : (int) C_STAT,
628 (int) DT_FCN << N_BTSHFT);
631 /* Keep a list of external functions. */
633 struct GTY(()) extern_list
635 struct extern_list *next;
636 tree decl;
637 const char *name;
640 static GTY(()) struct extern_list *extern_head;
642 /* Assemble an external function reference. We need to keep a list of
643 these, so that we can output the function types at the end of the
644 assembly. We can't output the types now, because we might see a
645 definition of the function later on and emit debugging information
646 for it then. */
648 void
649 i386_pe_record_external_function (tree decl, const char *name)
651 struct extern_list *p;
653 p = ggc_alloc<extern_list> ();
654 p->next = extern_head;
655 p->decl = decl;
656 p->name = name;
657 extern_head = p;
660 /* Keep a list of exported symbols. */
662 struct GTY(()) export_list
664 struct export_list *next;
665 const char *name;
666 int is_data; /* used to type tag exported symbols. */
669 /* Keep a list of stub symbols. */
671 struct GTY(()) stub_list
673 struct stub_list *next;
674 const char *name;
677 static GTY(()) struct export_list *export_head;
679 static GTY(()) struct stub_list *stub_head;
681 /* Assemble an export symbol entry. We need to keep a list of
682 these, so that we can output the export list at the end of the
683 assembly. We used to output these export symbols in each function,
684 but that causes problems with GNU ld when the sections are
685 linkonce. Beware, DECL may be NULL if compile_file() is emitting
686 the LTO marker. */
688 void
689 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
691 rtx symbol;
692 struct export_list *p;
694 if (!decl)
695 return;
697 symbol = XEXP (DECL_RTL (decl), 0);
698 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
699 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
700 return;
702 gcc_assert (TREE_PUBLIC (decl));
704 p = ggc_alloc<export_list> ();
705 p->next = export_head;
706 p->name = name;
707 p->is_data = is_data;
708 export_head = p;
711 void
712 i386_pe_record_stub (const char *name)
714 struct stub_list *p;
716 if (!name || *name == 0)
717 return;
719 p = stub_head;
720 while (p != NULL)
722 if (p->name[0] == *name
723 && !strcmp (p->name, name))
724 return;
725 p = p->next;
728 p = ggc_alloc<stub_list> ();
729 p->next = stub_head;
730 p->name = name;
731 stub_head = p;
735 #ifdef CXX_WRAP_SPEC_LIST
737 /* Hashtable helpers. */
739 struct wrapped_symbol_hasher : typed_noop_remove <char>
741 typedef char *value_type;
742 typedef char *compare_type;
743 static inline hashval_t hash (const char *);
744 static inline bool equal (const char *, const char *);
745 static inline void remove (char *);
748 inline hashval_t
749 wrapped_symbol_hasher::hash (const char *v)
751 return htab_hash_string (v);
754 /* Hash table equality helper function. */
756 inline bool
757 wrapped_symbol_hasher::equal (const char *x, const char *y)
759 return !strcmp (x, y);
762 /* Search for a function named TARGET in the list of library wrappers
763 we are using, returning a pointer to it if found or NULL if not.
764 This function might be called on quite a few symbols, and we only
765 have the list of names of wrapped functions available to us as a
766 spec string, so first time round we lazily initialise a hash table
767 to make things quicker. */
769 static const char *
770 i386_find_on_wrapper_list (const char *target)
772 static char first_time = 1;
773 static hash_table<wrapped_symbol_hasher> *wrappers;
775 if (first_time)
777 /* Beware that this is not a complicated parser, it assumes
778 that any sequence of non-whitespace beginning with an
779 underscore is one of the wrapped symbols. For now that's
780 adequate to distinguish symbols from spec substitutions
781 and command-line options. */
782 static char wrapper_list_buffer[] = CXX_WRAP_SPEC_LIST;
783 char *bufptr;
784 /* Breaks up the char array into separated strings
785 strings and enter them into the hash table. */
786 wrappers = new hash_table<wrapped_symbol_hasher> (8);
787 for (bufptr = wrapper_list_buffer; *bufptr; ++bufptr)
789 char *found = NULL;
790 if (ISSPACE (*bufptr))
791 continue;
792 if (*bufptr == '_')
793 found = bufptr;
794 while (*bufptr && !ISSPACE (*bufptr))
795 ++bufptr;
796 if (*bufptr)
797 *bufptr = 0;
798 if (found)
799 *wrappers->find_slot (found, INSERT) = found;
801 first_time = 0;
804 return wrappers->find (target);
807 #endif /* CXX_WRAP_SPEC_LIST */
809 /* This is called at the end of assembly. For each external function
810 which has not been defined, we output a declaration now. We also
811 output the .drectve section. */
813 void
814 i386_pe_file_end (void)
816 struct extern_list *p;
818 for (p = extern_head; p != NULL; p = p->next)
820 tree decl;
822 decl = p->decl;
824 /* Positively ensure only one declaration for any given symbol. */
825 if (! TREE_ASM_WRITTEN (decl)
826 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
828 #ifdef CXX_WRAP_SPEC_LIST
829 /* To ensure the DLL that provides the corresponding real
830 functions is still loaded at runtime, we must reference
831 the real function so that an (unused) import is created. */
832 const char *realsym = i386_find_on_wrapper_list (p->name);
833 if (realsym)
834 i386_pe_declare_function_type (asm_out_file,
835 concat ("__real_", realsym, NULL), TREE_PUBLIC (decl));
836 #endif /* CXX_WRAP_SPEC_LIST */
837 TREE_ASM_WRITTEN (decl) = 1;
838 i386_pe_declare_function_type (asm_out_file, p->name,
839 TREE_PUBLIC (decl));
843 if (export_head)
845 struct export_list *q;
846 drectve_section ();
847 for (q = export_head; q != NULL; q = q->next)
849 fprintf (asm_out_file, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
850 default_strip_name_encoding (q->name),
851 (q->is_data ? ",data" : ""));
855 if (stub_head)
857 struct stub_list *q;
859 for (q = stub_head; q != NULL; q = q->next)
861 const char *name = q->name;
862 const char *oname;
864 if (name[0] == '*')
865 ++name;
866 oname = name;
867 if (name[0] == '.')
868 ++name;
869 if (strncmp (name, "refptr.", 7) != 0)
870 continue;
871 name += 7;
872 fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n"
873 "\t.globl\t%s\n"
874 "\t.linkonce\tdiscard\n", oname, oname);
875 fprintf (asm_out_file, "%s:\n\t.quad\t%s\n", oname, name);
881 /* x64 Structured Exception Handling unwind info. */
883 struct seh_frame_state
885 /* SEH records saves relative to the "current" stack pointer, whether
886 or not there's a frame pointer in place. This tracks the current
887 stack pointer offset from the CFA. */
888 HOST_WIDE_INT sp_offset;
890 /* The CFA is located at CFA_REG + CFA_OFFSET. */
891 HOST_WIDE_INT cfa_offset;
892 rtx cfa_reg;
895 /* Set up data structures beginning output for SEH. */
897 void
898 i386_pe_seh_init (FILE *f)
900 struct seh_frame_state *seh;
902 if (!TARGET_SEH)
903 return;
904 if (cfun->is_thunk)
905 return;
907 /* We cannot support DRAP with SEH. We turned off support for it by
908 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
909 gcc_assert (!stack_realign_drap);
911 seh = XCNEW (struct seh_frame_state);
912 cfun->machine->seh = seh;
914 seh->sp_offset = INCOMING_FRAME_SP_OFFSET;
915 seh->cfa_offset = INCOMING_FRAME_SP_OFFSET;
916 seh->cfa_reg = stack_pointer_rtx;
918 fputs ("\t.seh_proc\t", f);
919 assemble_name (f, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun->decl)));
920 fputc ('\n', f);
923 void
924 i386_pe_seh_end_prologue (FILE *f)
926 struct seh_frame_state *seh;
928 if (!TARGET_SEH)
929 return;
930 if (cfun->is_thunk)
931 return;
932 seh = cfun->machine->seh;
934 XDELETE (seh);
935 cfun->machine->seh = NULL;
937 fputs ("\t.seh_endprologue\n", f);
940 static void
941 i386_pe_seh_fini (FILE *f)
943 if (!TARGET_SEH)
944 return;
945 if (cfun->is_thunk)
946 return;
947 fputs ("\t.seh_endproc\n", f);
950 /* Emit an assembler directive to save REG via a PUSH. */
952 static void
953 seh_emit_push (FILE *f, struct seh_frame_state *seh, rtx reg)
955 unsigned int regno = REGNO (reg);
957 gcc_checking_assert (GENERAL_REGNO_P (regno));
959 seh->sp_offset += UNITS_PER_WORD;
960 if (seh->cfa_reg == stack_pointer_rtx)
961 seh->cfa_offset += UNITS_PER_WORD;
963 fputs ("\t.seh_pushreg\t", f);
964 print_reg (reg, 0, f);
965 fputc ('\n', f);
968 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
970 static void
971 seh_emit_save (FILE *f, struct seh_frame_state *seh,
972 rtx reg, HOST_WIDE_INT cfa_offset)
974 unsigned int regno = REGNO (reg);
975 HOST_WIDE_INT offset;
977 /* Negative save offsets are of course not supported, since that
978 would be a store below the stack pointer and thus clobberable. */
979 gcc_assert (seh->sp_offset >= cfa_offset);
980 offset = seh->sp_offset - cfa_offset;
982 fputs ((SSE_REGNO_P (regno) ? "\t.seh_savexmm\t"
983 : GENERAL_REGNO_P (regno) ? "\t.seh_savereg\t"
984 : (gcc_unreachable (), "")), f);
985 print_reg (reg, 0, f);
986 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
989 /* Emit an assembler directive to adjust RSP by OFFSET. */
991 static void
992 seh_emit_stackalloc (FILE *f, struct seh_frame_state *seh,
993 HOST_WIDE_INT offset)
995 /* We're only concerned with prologue stack allocations, which all
996 are subtractions from the stack pointer. */
997 gcc_assert (offset < 0);
998 offset = -offset;
1000 if (seh->cfa_reg == stack_pointer_rtx)
1001 seh->cfa_offset += offset;
1002 seh->sp_offset += offset;
1004 /* Do not output the stackalloc in that case (it won't work as there is no
1005 encoding for very large frame size). */
1006 if (offset < SEH_MAX_FRAME_SIZE)
1007 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
1010 /* Process REG_CFA_ADJUST_CFA for SEH. */
1012 static void
1013 seh_cfa_adjust_cfa (FILE *f, struct seh_frame_state *seh, rtx pat)
1015 rtx dest, src;
1016 HOST_WIDE_INT reg_offset = 0;
1017 unsigned int dest_regno;
1019 dest = SET_DEST (pat);
1020 src = SET_SRC (pat);
1022 if (GET_CODE (src) == PLUS)
1024 reg_offset = INTVAL (XEXP (src, 1));
1025 src = XEXP (src, 0);
1027 else if (GET_CODE (src) == MINUS)
1029 reg_offset = -INTVAL (XEXP (src, 1));
1030 src = XEXP (src, 0);
1032 gcc_assert (src == stack_pointer_rtx);
1033 gcc_assert (seh->cfa_reg == stack_pointer_rtx);
1034 dest_regno = REGNO (dest);
1036 if (dest_regno == STACK_POINTER_REGNUM)
1037 seh_emit_stackalloc (f, seh, reg_offset);
1038 else if (dest_regno == HARD_FRAME_POINTER_REGNUM)
1040 HOST_WIDE_INT offset;
1042 seh->cfa_reg = dest;
1043 seh->cfa_offset -= reg_offset;
1045 offset = seh->sp_offset - seh->cfa_offset;
1047 gcc_assert ((offset & 15) == 0);
1048 gcc_assert (IN_RANGE (offset, 0, 240));
1050 fputs ("\t.seh_setframe\t", f);
1051 print_reg (seh->cfa_reg, 0, f);
1052 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
1054 else
1055 gcc_unreachable ();
1058 /* Process REG_CFA_OFFSET for SEH. */
1060 static void
1061 seh_cfa_offset (FILE *f, struct seh_frame_state *seh, rtx pat)
1063 rtx dest, src;
1064 HOST_WIDE_INT reg_offset;
1066 dest = SET_DEST (pat);
1067 src = SET_SRC (pat);
1069 gcc_assert (MEM_P (dest));
1070 dest = XEXP (dest, 0);
1071 if (REG_P (dest))
1072 reg_offset = 0;
1073 else
1075 gcc_assert (GET_CODE (dest) == PLUS);
1076 reg_offset = INTVAL (XEXP (dest, 1));
1077 dest = XEXP (dest, 0);
1079 gcc_assert (dest == seh->cfa_reg);
1081 seh_emit_save (f, seh, src, seh->cfa_offset - reg_offset);
1084 /* Process a FRAME_RELATED_EXPR for SEH. */
1086 static void
1087 seh_frame_related_expr (FILE *f, struct seh_frame_state *seh, rtx pat)
1089 rtx dest, src;
1090 HOST_WIDE_INT addend;
1092 /* See the full loop in dwarf2out_frame_debug_expr. */
1093 if (GET_CODE (pat) == PARALLEL || GET_CODE (pat) == SEQUENCE)
1095 int i, n = XVECLEN (pat, 0), pass, npass;
1097 npass = (GET_CODE (pat) == PARALLEL ? 2 : 1);
1098 for (pass = 0; pass < npass; ++pass)
1099 for (i = 0; i < n; ++i)
1101 rtx ele = XVECEXP (pat, 0, i);
1103 if (GET_CODE (ele) != SET)
1104 continue;
1105 dest = SET_DEST (ele);
1107 /* Process each member of the PARALLEL independently. The first
1108 member is always processed; others only if they are marked. */
1109 if (i == 0 || RTX_FRAME_RELATED_P (ele))
1111 /* Evaluate all register saves in the first pass and all
1112 register updates in the second pass. */
1113 if ((MEM_P (dest) ^ pass) || npass == 1)
1114 seh_frame_related_expr (f, seh, ele);
1117 return;
1120 dest = SET_DEST (pat);
1121 src = SET_SRC (pat);
1123 switch (GET_CODE (dest))
1125 case REG:
1126 switch (GET_CODE (src))
1128 case REG:
1129 /* REG = REG: This should be establishing a frame pointer. */
1130 gcc_assert (src == stack_pointer_rtx);
1131 gcc_assert (dest == hard_frame_pointer_rtx);
1132 seh_cfa_adjust_cfa (f, seh, pat);
1133 break;
1135 case PLUS:
1136 addend = INTVAL (XEXP (src, 1));
1137 src = XEXP (src, 0);
1138 if (dest == hard_frame_pointer_rtx)
1139 seh_cfa_adjust_cfa (f, seh, pat);
1140 else if (dest == stack_pointer_rtx)
1142 gcc_assert (src == stack_pointer_rtx);
1143 seh_emit_stackalloc (f, seh, addend);
1145 else
1146 gcc_unreachable ();
1147 break;
1149 default:
1150 gcc_unreachable ();
1152 break;
1154 case MEM:
1155 /* A save of some kind. */
1156 dest = XEXP (dest, 0);
1157 if (GET_CODE (dest) == PRE_DEC)
1159 gcc_checking_assert (GET_MODE (src) == Pmode);
1160 gcc_checking_assert (REG_P (src));
1161 seh_emit_push (f, seh, src);
1163 else
1164 seh_cfa_offset (f, seh, pat);
1165 break;
1167 default:
1168 gcc_unreachable ();
1172 /* This function looks at a single insn and emits any SEH directives
1173 required for unwind of this insn. */
1175 void
1176 i386_pe_seh_unwind_emit (FILE *asm_out_file, rtx_insn *insn)
1178 rtx note, pat;
1179 bool handled_one = false;
1180 struct seh_frame_state *seh;
1182 if (!TARGET_SEH)
1183 return;
1185 /* We free the SEH data once done with the prologue. Ignore those
1186 RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1187 seh = cfun->machine->seh;
1188 if (seh == NULL)
1189 return;
1191 if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
1192 return;
1194 for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
1196 switch (REG_NOTE_KIND (note))
1198 case REG_FRAME_RELATED_EXPR:
1199 pat = XEXP (note, 0);
1200 goto found;
1202 case REG_CFA_DEF_CFA:
1203 case REG_CFA_EXPRESSION:
1204 /* Only emitted with DRAP, which we disable. */
1205 gcc_unreachable ();
1206 break;
1208 case REG_CFA_REGISTER:
1209 /* Only emitted in epilogues, which we skip. */
1210 gcc_unreachable ();
1212 case REG_CFA_ADJUST_CFA:
1213 pat = XEXP (note, 0);
1214 if (pat == NULL)
1216 pat = PATTERN (insn);
1217 if (GET_CODE (pat) == PARALLEL)
1218 pat = XVECEXP (pat, 0, 0);
1220 seh_cfa_adjust_cfa (asm_out_file, seh, pat);
1221 handled_one = true;
1222 break;
1224 case REG_CFA_OFFSET:
1225 pat = XEXP (note, 0);
1226 if (pat == NULL)
1227 pat = single_set (insn);
1228 seh_cfa_offset (asm_out_file, seh, pat);
1229 handled_one = true;
1230 break;
1232 default:
1233 break;
1236 if (handled_one)
1237 return;
1238 pat = PATTERN (insn);
1239 found:
1240 seh_frame_related_expr (asm_out_file, seh, pat);
1243 void
1244 i386_pe_seh_emit_except_personality (rtx personality)
1246 int flags = 0;
1248 if (!TARGET_SEH)
1249 return;
1251 fputs ("\t.seh_handler\t", asm_out_file);
1252 output_addr_const (asm_out_file, personality);
1254 #if 0
1255 /* ??? The current implementation of _GCC_specific_handler requires
1256 both except and unwind handling, regardless of which sorts the
1257 user-level function requires. */
1258 eh_region r;
1259 FOR_ALL_EH_REGION(r)
1261 if (r->type == ERT_CLEANUP)
1262 flags |= 1;
1263 else
1264 flags |= 2;
1266 #else
1267 flags = 3;
1268 #endif
1270 if (flags & 1)
1271 fputs (", @unwind", asm_out_file);
1272 if (flags & 2)
1273 fputs (", @except", asm_out_file);
1274 fputc ('\n', asm_out_file);
1277 void
1278 i386_pe_seh_init_sections (void)
1280 if (TARGET_SEH)
1281 exception_section = get_unnamed_section (0, output_section_asm_op,
1282 "\t.seh_handlerdata");
1285 void
1286 i386_pe_start_function (FILE *f, const char *name, tree decl)
1288 i386_pe_maybe_record_exported_symbol (decl, name, 0);
1289 if (write_symbols != SDB_DEBUG)
1290 i386_pe_declare_function_type (f, name, TREE_PUBLIC (decl));
1291 /* In case section was altered by debugging output. */
1292 if (decl != NULL_TREE)
1293 switch_to_section (function_section (decl));
1294 ASM_OUTPUT_FUNCTION_LABEL (f, name, decl);
1297 void
1298 i386_pe_end_function (FILE *f, const char *, tree)
1300 i386_pe_seh_fini (f);
1304 #include "gt-winnt.h"