[AVX-512] Enable QI-mode mask logic patterns on non-AVX-512DQ targets.
[official-gcc.git] / gcc / config / i386 / winnt.c
blobec670cf4bfe8ce928dc692df9e7b237b02901caf
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 "target.h"
25 #include "function.h"
26 #include "basic-block.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "gimple.h"
30 #include "tm_p.h"
31 #include "stringpool.h"
32 #include "emit-rtl.h"
33 #include "cgraph.h"
34 #include "lto-streamer.h"
35 #include "output.h"
36 #include "varasm.h"
37 #include "lto-section-names.h"
39 /* i386/PE specific attribute support.
41 i386/PE has two new attributes:
42 dllexport - for exporting a function/variable that will live in a dll
43 dllimport - for importing a function/variable from a dll
45 Microsoft allows multiple declspecs in one __declspec, separating
46 them with spaces. We do NOT support this. Instead, use __declspec
47 multiple times.
50 /* Handle a "shared" attribute;
51 arguments as in struct attribute_spec.handler. */
52 tree
53 ix86_handle_shared_attribute (tree *node, tree name, tree, int,
54 bool *no_add_attrs)
56 if (TREE_CODE (*node) != VAR_DECL)
58 warning (OPT_Wattributes, "%qE attribute only applies to variables",
59 name);
60 *no_add_attrs = true;
63 return NULL_TREE;
66 /* Handle a "selectany" attribute;
67 arguments as in struct attribute_spec.handler. */
68 tree
69 ix86_handle_selectany_attribute (tree *node, tree name, tree, int,
70 bool *no_add_attrs)
72 tree decl = *node;
73 /* The attribute applies only to objects that are initialized and have
74 external linkage. However, we may not know about initialization
75 until the language frontend has processed the decl. Therefore
76 we make sure that variable isn't initialized as common. */
77 if (TREE_CODE (decl) != VAR_DECL || !TREE_PUBLIC (decl))
78 error ("%qE attribute applies only to initialized variables"
79 " with external linkage", name);
80 else
82 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
83 /* A variable with attribute selectany never can be common. */
84 DECL_COMMON (decl) = 0;
87 /* We don't need to keep attribute itself. */
88 *no_add_attrs = true;
89 return NULL_TREE;
93 /* Return the type that we should use to determine if DECL is
94 imported or exported. */
96 static tree
97 associated_type (tree decl)
99 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
100 ? DECL_CONTEXT (decl) : NULL_TREE);
103 /* Return true if DECL should be a dllexport'd object. */
105 static bool
106 i386_pe_determine_dllexport_p (tree decl)
108 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
109 return false;
111 /* Don't export local clones of dllexports. */
112 if (!TREE_PUBLIC (decl))
113 return false;
115 if (TREE_CODE (decl) == FUNCTION_DECL
116 && DECL_DECLARED_INLINE_P (decl)
117 && !flag_keep_inline_dllexport)
118 return false;
120 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
121 return true;
123 return false;
126 /* Return true if DECL should be a dllimport'd object. */
128 static bool
129 i386_pe_determine_dllimport_p (tree decl)
131 tree assoc;
133 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
134 return false;
136 if (DECL_DLLIMPORT_P (decl))
137 return true;
139 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
140 by targetm.cxx.adjust_class_at_definition. Check again to emit
141 error message if the class attribute has been overridden by an
142 out-of-class definition of static data. */
143 assoc = associated_type (decl);
144 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc))
145 && TREE_CODE (decl) == VAR_DECL
146 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
147 && !DECL_EXTERNAL (decl)
148 /* vtable's are linkonce constants, so defining a vtable is not
149 an error as long as we don't try to import it too. */
150 && !DECL_VIRTUAL_P (decl))
151 error ("definition of static data member %q+D of "
152 "dllimport%'d class", decl);
154 return false;
157 /* Handle the -mno-fun-dllimport target switch. */
159 bool
160 i386_pe_valid_dllimport_attribute_p (const_tree decl)
162 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
163 return false;
164 return true;
167 /* Return string which is the function name, identified by ID, modified
168 with a suffix consisting of an atsign (@) followed by the number of
169 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
170 FASTCALL is true, also add the FASTCALL_PREFIX.
171 Return NULL if no change required. */
173 static tree
174 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
176 HOST_WIDE_INT total = 0;
177 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
178 char *new_str, *p;
179 tree type = TREE_TYPE (DECL_ORIGIN (decl));
180 tree arg;
181 function_args_iterator args_iter;
183 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
185 if (prototype_p (type))
187 /* This attribute is ignored for variadic functions. */
188 if (stdarg_p (type))
189 return NULL_TREE;
191 /* Quit if we hit an incomplete type. Error is reported
192 by convert_arguments in c-typeck.c or cp/typeck.c. */
193 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
195 HOST_WIDE_INT parm_size;
196 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
198 if (! COMPLETE_TYPE_P (arg))
199 break;
201 parm_size = int_size_in_bytes (arg);
202 if (parm_size < 0)
203 break;
205 /* Must round up to include padding. This is done the same
206 way as in store_one_arg. */
207 parm_size = ((parm_size + parm_boundary_bytes - 1)
208 / parm_boundary_bytes * parm_boundary_bytes);
209 total += parm_size;
213 /* Assume max of 8 base 10 digits in the suffix. */
214 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
215 if (fastcall)
216 *p++ = FASTCALL_PREFIX;
217 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
219 return get_identifier (new_str);
222 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
223 fastcall function. The original identifier is supplied in ID. */
225 static tree
226 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
228 tree new_id = NULL_TREE;
230 if (TREE_CODE (decl) == FUNCTION_DECL)
232 unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
233 if ((ccvt & IX86_CALLCVT_STDCALL) != 0)
235 if (TARGET_RTD)
236 /* If we are using -mrtd emit undecorated symbol and let linker
237 do the proper resolving. */
238 return NULL_TREE;
239 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
241 else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
242 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
245 return new_id;
248 /* Emit an assembler directive to set symbol for DECL visibility to
249 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
250 As for PE there is no hidden support in gas, we just warn for
251 user-specified visibility attributes. */
253 void
254 i386_pe_assemble_visibility (tree decl, int)
256 if (!decl
257 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
258 return;
259 if (!DECL_ARTIFICIAL (decl))
260 warning (OPT_Wattributes, "visibility attribute not supported "
261 "in this configuration; ignored");
264 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
265 in the language-independent default hook
266 langhooks,c:lhd_set_decl_assembler_name ()
267 and in cp/mangle,c:mangle_decl (). */
268 tree
269 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
271 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
273 return (new_id ? new_id : id);
276 /* This hook behaves the same as varasm.c/assemble_name(), but
277 generates the name into memory rather than outputting it to
278 a file stream. */
280 tree
281 i386_pe_mangle_assembler_name (const char *name)
283 const char *skipped = name + (*name == '*' ? 1 : 0);
284 const char *stripped = targetm.strip_name_encoding (skipped);
285 if (*name != '*' && *user_label_prefix && *stripped != FASTCALL_PREFIX)
286 stripped = ACONCAT ((user_label_prefix, stripped, NULL));
287 return get_identifier (stripped);
290 void
291 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
293 rtx symbol;
294 int flags;
296 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
297 default_encode_section_info (decl, rtl, first);
299 /* Careful not to prod global register variables. */
300 if (!MEM_P (rtl))
301 return;
303 symbol = XEXP (rtl, 0);
304 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
306 switch (TREE_CODE (decl))
308 case FUNCTION_DECL:
309 case VAR_DECL:
310 break;
312 default:
313 return;
316 /* Mark the decl so we can tell from the rtl whether the object is
317 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
318 handles dllexport/dllimport override semantics. */
319 flags = (SYMBOL_REF_FLAGS (symbol) &
320 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
321 if (i386_pe_determine_dllexport_p (decl))
322 flags |= SYMBOL_FLAG_DLLEXPORT;
323 else if (i386_pe_determine_dllimport_p (decl))
324 flags |= SYMBOL_FLAG_DLLIMPORT;
326 SYMBOL_REF_FLAGS (symbol) = flags;
330 bool
331 i386_pe_binds_local_p (const_tree exp)
333 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
334 && DECL_DLLIMPORT_P (exp))
335 return false;
337 /* External public symbols, which aren't weakref-s,
338 have local-binding for PE targets. */
339 if (DECL_P (exp)
340 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
341 && TREE_PUBLIC (exp)
342 && DECL_EXTERNAL (exp))
343 return true;
344 return default_binds_local_p_1 (exp, 0);
347 /* Also strip the fastcall prefix and stdcall suffix. */
349 const char *
350 i386_pe_strip_name_encoding_full (const char *str)
352 const char *p;
353 const char *name = default_strip_name_encoding (str);
355 /* Strip leading '@' on fastcall symbols. */
356 if (*name == '@')
357 name++;
359 /* Strip trailing "@n". */
360 p = strchr (name, '@');
361 if (p)
362 return ggc_alloc_string (name, p - name);
364 return name;
367 void
368 i386_pe_unique_section (tree decl, int reloc)
370 int len;
371 const char *name, *prefix;
372 char *string;
374 /* Ignore RELOC, if we are allowed to put relocated
375 const data into read-only section. */
376 if (!flag_writable_rel_rdata)
377 reloc = 0;
378 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
379 name = i386_pe_strip_name_encoding_full (name);
381 /* The object is put in, for example, section .text$foo.
382 The linker will then ultimately place them in .text
383 (everything from the $ on is stripped). Don't put
384 read-only data in .rdata section to avoid a PE linker
385 bug when .rdata$* grouped sections are used in code
386 without a .rdata section. */
387 if (TREE_CODE (decl) == FUNCTION_DECL)
388 prefix = ".text$";
389 else if (decl_readonly_section (decl, reloc))
390 prefix = ".rdata$";
391 else
392 prefix = ".data$";
393 len = strlen (name) + strlen (prefix);
394 string = XALLOCAVEC (char, len + 1);
395 sprintf (string, "%s%s", prefix, name);
397 set_decl_section_name (decl, string);
400 /* Local and global relocs can be placed always into readonly memory for
401 memory for PE-COFF targets. */
403 i386_pe_reloc_rw_mask (void)
405 return 0;
408 /* Select a set of attributes for section NAME based on the properties
409 of DECL and whether or not RELOC indicates that DECL's initializer
410 might contain runtime relocations.
412 We make the section read-only and executable for a function decl,
413 read-only for a const data decl, and writable for a non-const data decl.
415 If the section has already been defined, to not allow it to have
416 different attributes, as (1) this is ambiguous since we're not seeing
417 all the declarations up front and (2) some assemblers (e.g. SVR4)
418 do not recognize section redefinitions. */
419 /* ??? This differs from the "standard" PE implementation in that we
420 handle the SHARED variable attribute. Should this be done for all
421 PE targets? */
423 #define SECTION_PE_SHARED SECTION_MACH_DEP
425 unsigned int
426 i386_pe_section_type_flags (tree decl, const char *, int reloc)
428 unsigned int flags;
430 /* Ignore RELOC, if we are allowed to put relocated
431 const data into read-only section. */
432 if (!flag_writable_rel_rdata)
433 reloc = 0;
435 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
436 flags = SECTION_CODE;
437 else if (decl && decl_readonly_section (decl, reloc))
438 flags = 0;
439 else
441 flags = SECTION_WRITE;
443 if (decl && TREE_CODE (decl) == VAR_DECL
444 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
445 flags |= SECTION_PE_SHARED;
448 if (decl && DECL_P (decl) && DECL_ONE_ONLY (decl))
449 flags |= SECTION_LINKONCE;
451 return flags;
454 void
455 i386_pe_asm_named_section (const char *name, unsigned int flags,
456 tree decl)
458 char flagchars[8], *f = flagchars;
460 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
461 if ((flags & SECTION_EXCLUDE) != 0)
462 *f++ = 'e';
463 #endif
465 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
466 /* readonly data */
468 *f++ ='d'; /* This is necessary for older versions of gas. */
469 *f++ ='r';
471 else
473 if (flags & SECTION_CODE)
474 *f++ = 'x';
475 if (flags & SECTION_WRITE)
476 *f++ = 'w';
477 if (flags & SECTION_PE_SHARED)
478 *f++ = 's';
479 #if !defined (HAVE_GAS_SECTION_EXCLUDE) || HAVE_GAS_SECTION_EXCLUDE == 0
480 /* If attribute "e" isn't supported we mark this section as
481 never-load. */
482 if ((flags & SECTION_EXCLUDE) != 0)
483 *f++ = 'n';
484 #endif
487 /* LTO sections need 1-byte alignment to avoid confusing the
488 zlib decompression algorithm with trailing zero pad bytes. */
489 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
490 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
491 *f++ = '0';
493 *f = '\0';
495 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
497 if (flags & SECTION_LINKONCE)
499 /* Functions may have been compiled at various levels of
500 optimization so we can't use `same_size' here.
501 Instead, have the linker pick one, without warning.
502 If 'selectany' attribute has been specified, MS compiler
503 sets 'discard' characteristic, rather than telling linker
504 to warn of size or content mismatch, so do the same. */
505 bool discard = (flags & SECTION_CODE)
506 || (TREE_CODE (decl) != IDENTIFIER_NODE
507 && lookup_attribute ("selectany",
508 DECL_ATTRIBUTES (decl)));
509 fprintf (asm_out_file, "\t.linkonce %s\n",
510 (discard ? "discard" : "same_size"));
514 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
516 void
517 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
518 const char *name, HOST_WIDE_INT size,
519 HOST_WIDE_INT align)
521 HOST_WIDE_INT rounded;
523 /* Compute as in assemble_noswitch_variable, since we don't have
524 support for aligned common on older binutils. We must also
525 avoid emitting a common symbol of size zero, as this is the
526 overloaded representation that indicates an undefined external
527 symbol in the PE object file format. */
528 rounded = size ? size : 1;
529 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
530 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
531 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
533 i386_pe_maybe_record_exported_symbol (decl, name, 1);
535 fprintf (stream, "\t.comm\t");
536 assemble_name (stream, name);
537 if (use_pe_aligned_common)
538 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
539 size ? size : HOST_WIDE_INT_1,
540 exact_log2 (align) - exact_log2 (CHAR_BIT));
541 else
542 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
543 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
546 /* The Microsoft linker requires that every function be marked as
547 DT_FCN. When using gas on cygwin, we must emit appropriate .type
548 directives. */
550 #include "gsyms.h"
552 /* Mark a function appropriately. This should only be called for
553 functions for which we are not emitting COFF debugging information.
554 FILE is the assembler output file, NAME is the name of the
555 function, and PUB is nonzero if the function is globally
556 visible. */
558 void
559 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
561 fprintf (file, "\t.def\t");
562 assemble_name (file, name);
563 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
564 pub ? (int) C_EXT : (int) C_STAT,
565 (int) DT_FCN << N_BTSHFT);
568 /* Keep a list of external functions. */
570 struct GTY(()) extern_list
572 struct extern_list *next;
573 tree decl;
574 const char *name;
577 static GTY(()) struct extern_list *extern_head;
579 /* Assemble an external function reference. We need to keep a list of
580 these, so that we can output the function types at the end of the
581 assembly. We can't output the types now, because we might see a
582 definition of the function later on and emit debugging information
583 for it then. */
585 void
586 i386_pe_record_external_function (tree decl, const char *name)
588 struct extern_list *p;
590 p = ggc_alloc<extern_list> ();
591 p->next = extern_head;
592 p->decl = decl;
593 p->name = name;
594 extern_head = p;
597 /* Keep a list of exported symbols. */
599 struct GTY(()) export_list
601 struct export_list *next;
602 const char *name;
603 int is_data; /* used to type tag exported symbols. */
606 /* Keep a list of stub symbols. */
608 struct GTY(()) stub_list
610 struct stub_list *next;
611 const char *name;
614 static GTY(()) struct export_list *export_head;
616 static GTY(()) struct stub_list *stub_head;
618 /* Assemble an export symbol entry. We need to keep a list of
619 these, so that we can output the export list at the end of the
620 assembly. We used to output these export symbols in each function,
621 but that causes problems with GNU ld when the sections are
622 linkonce. Beware, DECL may be NULL if compile_file() is emitting
623 the LTO marker. */
625 void
626 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
628 rtx symbol;
629 struct export_list *p;
631 if (!decl)
632 return;
634 symbol = XEXP (DECL_RTL (decl), 0);
635 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
636 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
637 return;
639 gcc_assert (TREE_PUBLIC (decl));
641 p = ggc_alloc<export_list> ();
642 p->next = export_head;
643 p->name = name;
644 p->is_data = is_data;
645 export_head = p;
648 void
649 i386_pe_record_stub (const char *name)
651 struct stub_list *p;
653 if (!name || *name == 0)
654 return;
656 p = stub_head;
657 while (p != NULL)
659 if (p->name[0] == *name
660 && !strcmp (p->name, name))
661 return;
662 p = p->next;
665 p = ggc_alloc<stub_list> ();
666 p->next = stub_head;
667 p->name = name;
668 stub_head = p;
672 #ifdef CXX_WRAP_SPEC_LIST
674 /* Search for a function named TARGET in the list of library wrappers
675 we are using, returning a pointer to it if found or NULL if not.
676 This function might be called on quite a few symbols, and we only
677 have the list of names of wrapped functions available to us as a
678 spec string, so first time round we lazily initialise a hash table
679 to make things quicker. */
681 static const char *
682 i386_find_on_wrapper_list (const char *target)
684 static char first_time = 1;
685 static hash_table<nofree_string_hash> *wrappers;
687 if (first_time)
689 /* Beware that this is not a complicated parser, it assumes
690 that any sequence of non-whitespace beginning with an
691 underscore is one of the wrapped symbols. For now that's
692 adequate to distinguish symbols from spec substitutions
693 and command-line options. */
694 static char wrapper_list_buffer[] = CXX_WRAP_SPEC_LIST;
695 char *bufptr;
696 /* Breaks up the char array into separated strings
697 strings and enter them into the hash table. */
698 wrappers = new hash_table<nofree_string_hash> (8);
699 for (bufptr = wrapper_list_buffer; *bufptr; ++bufptr)
701 char *found = NULL;
702 if (ISSPACE (*bufptr))
703 continue;
704 if (*bufptr == '_')
705 found = bufptr;
706 while (*bufptr && !ISSPACE (*bufptr))
707 ++bufptr;
708 if (*bufptr)
709 *bufptr = 0;
710 if (found)
711 *wrappers->find_slot (found, INSERT) = found;
713 first_time = 0;
716 return wrappers->find (target);
719 #endif /* CXX_WRAP_SPEC_LIST */
721 /* This is called at the end of assembly. For each external function
722 which has not been defined, we output a declaration now. We also
723 output the .drectve section. */
725 void
726 i386_pe_file_end (void)
728 struct extern_list *p;
730 for (p = extern_head; p != NULL; p = p->next)
732 tree decl;
734 decl = p->decl;
736 /* Positively ensure only one declaration for any given symbol. */
737 if (! TREE_ASM_WRITTEN (decl)
738 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
740 #ifdef CXX_WRAP_SPEC_LIST
741 /* To ensure the DLL that provides the corresponding real
742 functions is still loaded at runtime, we must reference
743 the real function so that an (unused) import is created. */
744 const char *realsym = i386_find_on_wrapper_list (p->name);
745 if (realsym)
746 i386_pe_declare_function_type (asm_out_file,
747 concat ("__real_", realsym, NULL), TREE_PUBLIC (decl));
748 #endif /* CXX_WRAP_SPEC_LIST */
749 TREE_ASM_WRITTEN (decl) = 1;
750 i386_pe_declare_function_type (asm_out_file, p->name,
751 TREE_PUBLIC (decl));
755 if (export_head)
757 struct export_list *q;
758 drectve_section ();
759 for (q = export_head; q != NULL; q = q->next)
761 fprintf (asm_out_file, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
762 default_strip_name_encoding (q->name),
763 (q->is_data ? ",data" : ""));
767 if (stub_head)
769 struct stub_list *q;
771 for (q = stub_head; q != NULL; q = q->next)
773 const char *name = q->name;
774 const char *oname;
776 if (name[0] == '*')
777 ++name;
778 oname = name;
779 if (name[0] == '.')
780 ++name;
781 if (strncmp (name, "refptr.", 7) != 0)
782 continue;
783 name += 7;
784 fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n"
785 "\t.globl\t%s\n"
786 "\t.linkonce\tdiscard\n", oname, oname);
787 fprintf (asm_out_file, "%s:\n\t.quad\t%s\n", oname, name);
793 /* x64 Structured Exception Handling unwind info. */
795 struct seh_frame_state
797 /* SEH records saves relative to the "current" stack pointer, whether
798 or not there's a frame pointer in place. This tracks the current
799 stack pointer offset from the CFA. */
800 HOST_WIDE_INT sp_offset;
802 /* The CFA is located at CFA_REG + CFA_OFFSET. */
803 HOST_WIDE_INT cfa_offset;
804 rtx cfa_reg;
807 /* Set up data structures beginning output for SEH. */
809 void
810 i386_pe_seh_init (FILE *f)
812 struct seh_frame_state *seh;
814 if (!TARGET_SEH)
815 return;
816 if (cfun->is_thunk)
817 return;
819 /* We cannot support DRAP with SEH. We turned off support for it by
820 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
821 gcc_assert (!stack_realign_drap);
823 seh = XCNEW (struct seh_frame_state);
824 cfun->machine->seh = seh;
826 seh->sp_offset = INCOMING_FRAME_SP_OFFSET;
827 seh->cfa_offset = INCOMING_FRAME_SP_OFFSET;
828 seh->cfa_reg = stack_pointer_rtx;
830 fputs ("\t.seh_proc\t", f);
831 assemble_name (f, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun->decl)));
832 fputc ('\n', f);
835 void
836 i386_pe_seh_end_prologue (FILE *f)
838 struct seh_frame_state *seh;
840 if (!TARGET_SEH)
841 return;
842 if (cfun->is_thunk)
843 return;
844 seh = cfun->machine->seh;
846 XDELETE (seh);
847 cfun->machine->seh = NULL;
849 fputs ("\t.seh_endprologue\n", f);
852 static void
853 i386_pe_seh_fini (FILE *f)
855 if (!TARGET_SEH)
856 return;
857 if (cfun->is_thunk)
858 return;
859 fputs ("\t.seh_endproc\n", f);
862 /* Emit an assembler directive to save REG via a PUSH. */
864 static void
865 seh_emit_push (FILE *f, struct seh_frame_state *seh, rtx reg)
867 unsigned int regno = REGNO (reg);
869 gcc_checking_assert (GENERAL_REGNO_P (regno));
871 seh->sp_offset += UNITS_PER_WORD;
872 if (seh->cfa_reg == stack_pointer_rtx)
873 seh->cfa_offset += UNITS_PER_WORD;
875 fputs ("\t.seh_pushreg\t", f);
876 print_reg (reg, 0, f);
877 fputc ('\n', f);
880 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
882 static void
883 seh_emit_save (FILE *f, struct seh_frame_state *seh,
884 rtx reg, HOST_WIDE_INT cfa_offset)
886 unsigned int regno = REGNO (reg);
887 HOST_WIDE_INT offset;
889 /* Negative save offsets are of course not supported, since that
890 would be a store below the stack pointer and thus clobberable. */
891 gcc_assert (seh->sp_offset >= cfa_offset);
892 offset = seh->sp_offset - cfa_offset;
894 fputs ((SSE_REGNO_P (regno) ? "\t.seh_savexmm\t"
895 : GENERAL_REGNO_P (regno) ? "\t.seh_savereg\t"
896 : (gcc_unreachable (), "")), f);
897 print_reg (reg, 0, f);
898 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
901 /* Emit an assembler directive to adjust RSP by OFFSET. */
903 static void
904 seh_emit_stackalloc (FILE *f, struct seh_frame_state *seh,
905 HOST_WIDE_INT offset)
907 /* We're only concerned with prologue stack allocations, which all
908 are subtractions from the stack pointer. */
909 gcc_assert (offset < 0);
910 offset = -offset;
912 if (seh->cfa_reg == stack_pointer_rtx)
913 seh->cfa_offset += offset;
914 seh->sp_offset += offset;
916 /* Do not output the stackalloc in that case (it won't work as there is no
917 encoding for very large frame size). */
918 if (offset < SEH_MAX_FRAME_SIZE)
919 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
922 /* Process REG_CFA_ADJUST_CFA for SEH. */
924 static void
925 seh_cfa_adjust_cfa (FILE *f, struct seh_frame_state *seh, rtx pat)
927 rtx dest, src;
928 HOST_WIDE_INT reg_offset = 0;
929 unsigned int dest_regno;
931 dest = SET_DEST (pat);
932 src = SET_SRC (pat);
934 if (GET_CODE (src) == PLUS)
936 reg_offset = INTVAL (XEXP (src, 1));
937 src = XEXP (src, 0);
939 else if (GET_CODE (src) == MINUS)
941 reg_offset = -INTVAL (XEXP (src, 1));
942 src = XEXP (src, 0);
944 gcc_assert (src == stack_pointer_rtx);
945 gcc_assert (seh->cfa_reg == stack_pointer_rtx);
946 dest_regno = REGNO (dest);
948 if (dest_regno == STACK_POINTER_REGNUM)
949 seh_emit_stackalloc (f, seh, reg_offset);
950 else if (dest_regno == HARD_FRAME_POINTER_REGNUM)
952 HOST_WIDE_INT offset;
954 seh->cfa_reg = dest;
955 seh->cfa_offset -= reg_offset;
957 offset = seh->sp_offset - seh->cfa_offset;
959 gcc_assert ((offset & 15) == 0);
960 gcc_assert (IN_RANGE (offset, 0, 240));
962 fputs ("\t.seh_setframe\t", f);
963 print_reg (seh->cfa_reg, 0, f);
964 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
966 else
967 gcc_unreachable ();
970 /* Process REG_CFA_OFFSET for SEH. */
972 static void
973 seh_cfa_offset (FILE *f, struct seh_frame_state *seh, rtx pat)
975 rtx dest, src;
976 HOST_WIDE_INT reg_offset;
978 dest = SET_DEST (pat);
979 src = SET_SRC (pat);
981 gcc_assert (MEM_P (dest));
982 dest = XEXP (dest, 0);
983 if (REG_P (dest))
984 reg_offset = 0;
985 else
987 gcc_assert (GET_CODE (dest) == PLUS);
988 reg_offset = INTVAL (XEXP (dest, 1));
989 dest = XEXP (dest, 0);
991 gcc_assert (dest == seh->cfa_reg);
993 seh_emit_save (f, seh, src, seh->cfa_offset - reg_offset);
996 /* Process a FRAME_RELATED_EXPR for SEH. */
998 static void
999 seh_frame_related_expr (FILE *f, struct seh_frame_state *seh, rtx pat)
1001 rtx dest, src;
1002 HOST_WIDE_INT addend;
1004 /* See the full loop in dwarf2out_frame_debug_expr. */
1005 if (GET_CODE (pat) == PARALLEL || GET_CODE (pat) == SEQUENCE)
1007 int i, n = XVECLEN (pat, 0), pass, npass;
1009 npass = (GET_CODE (pat) == PARALLEL ? 2 : 1);
1010 for (pass = 0; pass < npass; ++pass)
1011 for (i = 0; i < n; ++i)
1013 rtx ele = XVECEXP (pat, 0, i);
1015 if (GET_CODE (ele) != SET)
1016 continue;
1017 dest = SET_DEST (ele);
1019 /* Process each member of the PARALLEL independently. The first
1020 member is always processed; others only if they are marked. */
1021 if (i == 0 || RTX_FRAME_RELATED_P (ele))
1023 /* Evaluate all register saves in the first pass and all
1024 register updates in the second pass. */
1025 if ((MEM_P (dest) ^ pass) || npass == 1)
1026 seh_frame_related_expr (f, seh, ele);
1029 return;
1032 dest = SET_DEST (pat);
1033 src = SET_SRC (pat);
1035 switch (GET_CODE (dest))
1037 case REG:
1038 switch (GET_CODE (src))
1040 case REG:
1041 /* REG = REG: This should be establishing a frame pointer. */
1042 gcc_assert (src == stack_pointer_rtx);
1043 gcc_assert (dest == hard_frame_pointer_rtx);
1044 seh_cfa_adjust_cfa (f, seh, pat);
1045 break;
1047 case PLUS:
1048 addend = INTVAL (XEXP (src, 1));
1049 src = XEXP (src, 0);
1050 if (dest == hard_frame_pointer_rtx)
1051 seh_cfa_adjust_cfa (f, seh, pat);
1052 else if (dest == stack_pointer_rtx)
1054 gcc_assert (src == stack_pointer_rtx);
1055 seh_emit_stackalloc (f, seh, addend);
1057 else
1058 gcc_unreachable ();
1059 break;
1061 default:
1062 gcc_unreachable ();
1064 break;
1066 case MEM:
1067 /* A save of some kind. */
1068 dest = XEXP (dest, 0);
1069 if (GET_CODE (dest) == PRE_DEC)
1071 gcc_checking_assert (GET_MODE (src) == Pmode);
1072 gcc_checking_assert (REG_P (src));
1073 seh_emit_push (f, seh, src);
1075 else
1076 seh_cfa_offset (f, seh, pat);
1077 break;
1079 default:
1080 gcc_unreachable ();
1084 /* This function looks at a single insn and emits any SEH directives
1085 required for unwind of this insn. */
1087 void
1088 i386_pe_seh_unwind_emit (FILE *asm_out_file, rtx_insn *insn)
1090 rtx note, pat;
1091 bool handled_one = false;
1092 struct seh_frame_state *seh;
1094 if (!TARGET_SEH)
1095 return;
1097 /* We free the SEH data once done with the prologue. Ignore those
1098 RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1099 seh = cfun->machine->seh;
1100 if (seh == NULL)
1101 return;
1103 if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
1104 return;
1106 for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
1108 switch (REG_NOTE_KIND (note))
1110 case REG_FRAME_RELATED_EXPR:
1111 pat = XEXP (note, 0);
1112 goto found;
1114 case REG_CFA_DEF_CFA:
1115 case REG_CFA_EXPRESSION:
1116 /* Only emitted with DRAP, which we disable. */
1117 gcc_unreachable ();
1118 break;
1120 case REG_CFA_REGISTER:
1121 /* Only emitted in epilogues, which we skip. */
1122 gcc_unreachable ();
1124 case REG_CFA_ADJUST_CFA:
1125 pat = XEXP (note, 0);
1126 if (pat == NULL)
1128 pat = PATTERN (insn);
1129 if (GET_CODE (pat) == PARALLEL)
1130 pat = XVECEXP (pat, 0, 0);
1132 seh_cfa_adjust_cfa (asm_out_file, seh, pat);
1133 handled_one = true;
1134 break;
1136 case REG_CFA_OFFSET:
1137 pat = XEXP (note, 0);
1138 if (pat == NULL)
1139 pat = single_set (insn);
1140 seh_cfa_offset (asm_out_file, seh, pat);
1141 handled_one = true;
1142 break;
1144 default:
1145 break;
1148 if (handled_one)
1149 return;
1150 pat = PATTERN (insn);
1151 found:
1152 seh_frame_related_expr (asm_out_file, seh, pat);
1155 void
1156 i386_pe_seh_emit_except_personality (rtx personality)
1158 int flags = 0;
1160 if (!TARGET_SEH)
1161 return;
1163 fputs ("\t.seh_handler\t", asm_out_file);
1164 output_addr_const (asm_out_file, personality);
1166 #if 0
1167 /* ??? The current implementation of _GCC_specific_handler requires
1168 both except and unwind handling, regardless of which sorts the
1169 user-level function requires. */
1170 eh_region r;
1171 FOR_ALL_EH_REGION(r)
1173 if (r->type == ERT_CLEANUP)
1174 flags |= 1;
1175 else
1176 flags |= 2;
1178 #else
1179 flags = 3;
1180 #endif
1182 if (flags & 1)
1183 fputs (", @unwind", asm_out_file);
1184 if (flags & 2)
1185 fputs (", @except", asm_out_file);
1186 fputc ('\n', asm_out_file);
1189 void
1190 i386_pe_seh_init_sections (void)
1192 if (TARGET_SEH)
1193 exception_section = get_unnamed_section (0, output_section_asm_op,
1194 "\t.seh_handlerdata");
1197 void
1198 i386_pe_start_function (FILE *f, const char *name, tree decl)
1200 i386_pe_maybe_record_exported_symbol (decl, name, 0);
1201 if (write_symbols != SDB_DEBUG)
1202 i386_pe_declare_function_type (f, name, TREE_PUBLIC (decl));
1203 /* In case section was altered by debugging output. */
1204 if (decl != NULL_TREE)
1205 switch_to_section (function_section (decl));
1206 ASM_OUTPUT_FUNCTION_LABEL (f, name, decl);
1209 void
1210 i386_pe_end_function (FILE *f, const char *, tree)
1212 i386_pe_seh_fini (f);
1216 #include "gt-winnt.h"