1 /* Subroutines for insn-output.c for Windows NT.
2 Contributed by Douglas Rupp (drupp@cs.washington.edu)
3 Copyright (C) 1995-2018 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
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
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 #define IN_TARGET_CODE 1
25 #include "coretypes.h"
28 #include "basic-block.h"
34 #include "stringpool.h"
38 #include "lto-streamer.h"
42 #include "lto-section-names.h"
44 /* i386/PE specific attribute support.
46 i386/PE has two new attributes:
47 dllexport - for exporting a function/variable that will live in a dll
48 dllimport - for importing a function/variable from a dll
50 Microsoft allows multiple declspecs in one __declspec, separating
51 them with spaces. We do NOT support this. Instead, use __declspec
55 /* Handle a "shared" attribute;
56 arguments as in struct attribute_spec.handler. */
58 ix86_handle_shared_attribute (tree
*node
, tree name
, tree
, int,
61 if (TREE_CODE (*node
) != VAR_DECL
)
63 warning (OPT_Wattributes
, "%qE attribute only applies to variables",
71 /* Handle a "selectany" attribute;
72 arguments as in struct attribute_spec.handler. */
74 ix86_handle_selectany_attribute (tree
*node
, tree name
, tree
, int,
78 /* The attribute applies only to objects that are initialized and have
79 external linkage. However, we may not know about initialization
80 until the language frontend has processed the decl. Therefore
81 we make sure that variable isn't initialized as common. */
82 if (TREE_CODE (decl
) != VAR_DECL
|| !TREE_PUBLIC (decl
))
83 error ("%qE attribute applies only to initialized variables"
84 " with external linkage", name
);
87 make_decl_one_only (decl
, DECL_ASSEMBLER_NAME (decl
));
88 /* A variable with attribute selectany never can be common. */
89 DECL_COMMON (decl
) = 0;
92 /* We don't need to keep attribute itself. */
98 /* Return the type that we should use to determine if DECL is
99 imported or exported. */
102 associated_type (tree decl
)
104 return (DECL_CONTEXT (decl
) && TYPE_P (DECL_CONTEXT (decl
))
105 ? DECL_CONTEXT (decl
) : NULL_TREE
);
108 /* Return true if DECL should be a dllexport'd object. */
111 i386_pe_determine_dllexport_p (tree decl
)
113 if (TREE_CODE (decl
) != VAR_DECL
&& TREE_CODE (decl
) != FUNCTION_DECL
)
116 /* Don't export local clones of dllexports. */
117 if (!TREE_PUBLIC (decl
))
120 if (TREE_CODE (decl
) == FUNCTION_DECL
121 && DECL_DECLARED_INLINE_P (decl
)
122 && !flag_keep_inline_dllexport
)
125 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
)))
131 /* Return true if DECL should be a dllimport'd object. */
134 i386_pe_determine_dllimport_p (tree decl
)
138 if (TREE_CODE (decl
) != VAR_DECL
&& TREE_CODE (decl
) != FUNCTION_DECL
)
141 if (DECL_DLLIMPORT_P (decl
))
144 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
145 by targetm.cxx.adjust_class_at_definition. Check again to emit
146 error message if the class attribute has been overridden by an
147 out-of-class definition of static data. */
148 assoc
= associated_type (decl
);
149 if (assoc
&& lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc
))
150 && TREE_CODE (decl
) == VAR_DECL
151 && TREE_STATIC (decl
) && TREE_PUBLIC (decl
)
152 && !DECL_EXTERNAL (decl
)
153 /* vtable's are linkonce constants, so defining a vtable is not
154 an error as long as we don't try to import it too. */
155 && !DECL_VIRTUAL_P (decl
))
156 error ("definition of static data member %q+D of "
157 "dllimport%'d class", decl
);
162 /* Handle the -mno-fun-dllimport target switch. */
165 i386_pe_valid_dllimport_attribute_p (const_tree decl
)
167 if (TARGET_NOP_FUN_DLLIMPORT
&& TREE_CODE (decl
) == FUNCTION_DECL
)
172 /* Return string which is the function name, identified by ID, modified
173 with a suffix consisting of an atsign (@) followed by the number of
174 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
175 FASTCALL is true, also add the FASTCALL_PREFIX.
176 Return NULL if no change required. */
179 gen_stdcall_or_fastcall_suffix (tree decl
, tree id
, bool fastcall
)
181 HOST_WIDE_INT total
= 0;
182 const char *old_str
= IDENTIFIER_POINTER (id
!= NULL_TREE
? id
: DECL_NAME (decl
));
184 tree type
= TREE_TYPE (DECL_ORIGIN (decl
));
186 function_args_iterator args_iter
;
188 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
190 if (prototype_p (type
))
192 /* This attribute is ignored for variadic functions. */
196 /* Quit if we hit an incomplete type. Error is reported
197 by convert_arguments in c-typeck.c or cp/typeck.c. */
198 FOREACH_FUNCTION_ARGS(type
, arg
, args_iter
)
200 HOST_WIDE_INT parm_size
;
201 HOST_WIDE_INT parm_boundary_bytes
= PARM_BOUNDARY
/ BITS_PER_UNIT
;
203 if (! COMPLETE_TYPE_P (arg
))
206 parm_size
= int_size_in_bytes (arg
);
210 /* Must round up to include padding. This is done the same
211 way as in store_one_arg. */
212 parm_size
= ((parm_size
+ parm_boundary_bytes
- 1)
213 / parm_boundary_bytes
* parm_boundary_bytes
);
218 /* Assume max of 8 base 10 digits in the suffix. */
219 p
= new_str
= XALLOCAVEC (char, 1 + strlen (old_str
) + 1 + 8 + 1);
221 *p
++ = FASTCALL_PREFIX
;
222 sprintf (p
, "%s@" HOST_WIDE_INT_PRINT_DEC
, old_str
, total
);
224 return get_identifier (new_str
);
227 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
228 fastcall function. The original identifier is supplied in ID. */
231 i386_pe_maybe_mangle_decl_assembler_name (tree decl
, tree id
)
233 tree new_id
= NULL_TREE
;
235 if (TREE_CODE (decl
) == FUNCTION_DECL
)
237 unsigned int ccvt
= ix86_get_callcvt (TREE_TYPE (decl
));
238 if ((ccvt
& IX86_CALLCVT_STDCALL
) != 0)
241 /* If we are using -mrtd emit undecorated symbol and let linker
242 do the proper resolving. */
244 new_id
= gen_stdcall_or_fastcall_suffix (decl
, id
, false);
246 else if ((ccvt
& IX86_CALLCVT_FASTCALL
) != 0)
247 new_id
= gen_stdcall_or_fastcall_suffix (decl
, id
, true);
253 /* Emit an assembler directive to set symbol for DECL visibility to
254 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
255 As for PE there is no hidden support in gas, we just warn for
256 user-specified visibility attributes. */
259 i386_pe_assemble_visibility (tree decl
, int)
262 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl
)))
264 if (!DECL_ARTIFICIAL (decl
))
265 warning (OPT_Wattributes
, "visibility attribute not supported "
266 "in this configuration; ignored");
269 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
270 in the language-independent default hook
271 langhooks,c:lhd_set_decl_assembler_name ()
272 and in cp/mangle,c:mangle_decl (). */
274 i386_pe_mangle_decl_assembler_name (tree decl
, tree id
)
276 tree new_id
= i386_pe_maybe_mangle_decl_assembler_name (decl
, id
);
278 return (new_id
? new_id
: id
);
281 /* This hook behaves the same as varasm.c/assemble_name(), but
282 generates the name into memory rather than outputting it to
286 i386_pe_mangle_assembler_name (const char *name
)
288 const char *skipped
= name
+ (*name
== '*' ? 1 : 0);
289 const char *stripped
= targetm
.strip_name_encoding (skipped
);
290 if (*name
!= '*' && *user_label_prefix
&& *stripped
!= FASTCALL_PREFIX
)
291 stripped
= ACONCAT ((user_label_prefix
, stripped
, NULL
));
292 return get_identifier (stripped
);
296 i386_pe_encode_section_info (tree decl
, rtx rtl
, int first
)
301 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
302 default_encode_section_info (decl
, rtl
, first
);
304 /* Careful not to prod global register variables. */
308 symbol
= XEXP (rtl
, 0);
309 gcc_assert (GET_CODE (symbol
) == SYMBOL_REF
);
311 switch (TREE_CODE (decl
))
321 /* Mark the decl so we can tell from the rtl whether the object is
322 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
323 handles dllexport/dllimport override semantics. */
324 flags
= (SYMBOL_REF_FLAGS (symbol
) &
325 ~(SYMBOL_FLAG_DLLIMPORT
| SYMBOL_FLAG_DLLEXPORT
));
326 if (i386_pe_determine_dllexport_p (decl
))
327 flags
|= SYMBOL_FLAG_DLLEXPORT
;
328 else if (i386_pe_determine_dllimport_p (decl
))
329 flags
|= SYMBOL_FLAG_DLLIMPORT
;
331 SYMBOL_REF_FLAGS (symbol
) = flags
;
336 i386_pe_binds_local_p (const_tree exp
)
338 if ((TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == FUNCTION_DECL
)
339 && DECL_DLLIMPORT_P (exp
))
342 /* External public symbols, which aren't weakref-s,
343 have local-binding for PE targets. */
345 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (exp
))
347 && DECL_EXTERNAL (exp
))
350 #ifndef MAKE_DECL_ONE_ONLY
351 /* PR target/66655: If a function has been marked as DECL_ONE_ONLY
352 but we do not the means to make it so, then do not allow it to
355 && TREE_CODE (exp
) == FUNCTION_DECL
357 && DECL_ONE_ONLY (exp
)
358 && ! DECL_EXTERNAL (exp
)
359 && DECL_DECLARED_INLINE_P (exp
))
363 return default_binds_local_p_1 (exp
, 0);
366 /* Also strip the fastcall prefix and stdcall suffix. */
369 i386_pe_strip_name_encoding_full (const char *str
)
372 const char *name
= default_strip_name_encoding (str
);
374 /* Strip leading '@' on fastcall symbols. */
378 /* Strip trailing "@n". */
379 p
= strchr (name
, '@');
381 return ggc_alloc_string (name
, p
- name
);
387 i386_pe_unique_section (tree decl
, int reloc
)
390 const char *name
, *prefix
;
393 /* Ignore RELOC, if we are allowed to put relocated
394 const data into read-only section. */
395 if (!flag_writable_rel_rdata
)
397 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
398 name
= i386_pe_strip_name_encoding_full (name
);
400 /* The object is put in, for example, section .text$foo.
401 The linker will then ultimately place them in .text
402 (everything from the $ on is stripped). Don't put
403 read-only data in .rdata section to avoid a PE linker
404 bug when .rdata$* grouped sections are used in code
405 without a .rdata section. */
406 if (TREE_CODE (decl
) == FUNCTION_DECL
)
408 else if (decl_readonly_section (decl
, reloc
))
412 len
= strlen (name
) + strlen (prefix
);
413 string
= XALLOCAVEC (char, len
+ 1);
414 sprintf (string
, "%s%s", prefix
, name
);
416 set_decl_section_name (decl
, string
);
419 /* Local and global relocs can be placed always into readonly memory for
420 memory for PE-COFF targets. */
422 i386_pe_reloc_rw_mask (void)
427 /* Select a set of attributes for section NAME based on the properties
428 of DECL and whether or not RELOC indicates that DECL's initializer
429 might contain runtime relocations.
431 We make the section read-only and executable for a function decl,
432 read-only for a const data decl, and writable for a non-const data decl.
434 If the section has already been defined, to not allow it to have
435 different attributes, as (1) this is ambiguous since we're not seeing
436 all the declarations up front and (2) some assemblers (e.g. SVR4)
437 do not recognize section redefinitions. */
438 /* ??? This differs from the "standard" PE implementation in that we
439 handle the SHARED variable attribute. Should this be done for all
442 #define SECTION_PE_SHARED SECTION_MACH_DEP
445 i386_pe_section_type_flags (tree decl
, const char *, int reloc
)
449 /* Ignore RELOC, if we are allowed to put relocated
450 const data into read-only section. */
451 if (!flag_writable_rel_rdata
)
454 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
455 flags
= SECTION_CODE
;
456 else if (decl
&& decl_readonly_section (decl
, reloc
))
460 flags
= SECTION_WRITE
;
462 if (decl
&& TREE_CODE (decl
) == VAR_DECL
463 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl
)))
464 flags
|= SECTION_PE_SHARED
;
467 if (decl
&& DECL_P (decl
) && DECL_ONE_ONLY (decl
))
468 flags
|= SECTION_LINKONCE
;
474 i386_pe_asm_named_section (const char *name
, unsigned int flags
,
477 char flagchars
[8], *f
= flagchars
;
479 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
480 if ((flags
& SECTION_EXCLUDE
) != 0)
484 if ((flags
& (SECTION_CODE
| SECTION_WRITE
)) == 0)
487 *f
++ ='d'; /* This is necessary for older versions of gas. */
492 if (flags
& SECTION_CODE
)
494 if (flags
& SECTION_WRITE
)
496 if (flags
& SECTION_PE_SHARED
)
498 #if !defined (HAVE_GAS_SECTION_EXCLUDE) || HAVE_GAS_SECTION_EXCLUDE == 0
499 /* If attribute "e" isn't supported we mark this section as
501 if ((flags
& SECTION_EXCLUDE
) != 0)
506 /* LTO sections need 1-byte alignment to avoid confusing the
507 zlib decompression algorithm with trailing zero pad bytes. */
508 if (strncmp (name
, LTO_SECTION_NAME_PREFIX
,
509 strlen (LTO_SECTION_NAME_PREFIX
)) == 0)
514 fprintf (asm_out_file
, "\t.section\t%s,\"%s\"\n", name
, flagchars
);
516 if (flags
& SECTION_LINKONCE
)
518 /* Functions may have been compiled at various levels of
519 optimization so we can't use `same_size' here.
520 Instead, have the linker pick one, without warning.
521 If 'selectany' attribute has been specified, MS compiler
522 sets 'discard' characteristic, rather than telling linker
523 to warn of size or content mismatch, so do the same. */
524 bool discard
= (flags
& SECTION_CODE
)
525 || (TREE_CODE (decl
) != IDENTIFIER_NODE
526 && lookup_attribute ("selectany",
527 DECL_ATTRIBUTES (decl
)));
528 fprintf (asm_out_file
, "\t.linkonce %s\n",
529 (discard
? "discard" : "same_size"));
533 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
536 i386_pe_asm_output_aligned_decl_common (FILE *stream
, tree decl
,
537 const char *name
, HOST_WIDE_INT size
,
540 HOST_WIDE_INT rounded
;
542 /* Compute as in assemble_noswitch_variable, since we don't have
543 support for aligned common on older binutils. We must also
544 avoid emitting a common symbol of size zero, as this is the
545 overloaded representation that indicates an undefined external
546 symbol in the PE object file format. */
547 rounded
= size
? size
: 1;
548 rounded
+= (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
) - 1;
549 rounded
= (rounded
/ (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
)
550 * (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
552 i386_pe_maybe_record_exported_symbol (decl
, name
, 1);
554 fprintf (stream
, "\t.comm\t");
555 assemble_name (stream
, name
);
556 if (use_pe_aligned_common
)
557 fprintf (stream
, ", " HOST_WIDE_INT_PRINT_DEC
", %d\n",
558 size
? size
: HOST_WIDE_INT_1
,
559 exact_log2 (align
) - exact_log2 (CHAR_BIT
));
561 fprintf (stream
, ", " HOST_WIDE_INT_PRINT_DEC
"\t" ASM_COMMENT_START
562 " " HOST_WIDE_INT_PRINT_DEC
"\n", rounded
, size
);
565 /* The Microsoft linker requires that every function be marked as
566 DT_FCN. When using gas on cygwin, we must emit appropriate .type
571 /* Mark a function appropriately. This should only be called for
572 functions for which we are not emitting COFF debugging information.
573 FILE is the assembler output file, NAME is the name of the
574 function, and PUB is nonzero if the function is globally
578 i386_pe_declare_function_type (FILE *file
, const char *name
, int pub
)
580 fprintf (file
, "\t.def\t");
581 assemble_name (file
, name
);
582 fprintf (file
, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
583 pub
? (int) C_EXT
: (int) C_STAT
,
584 (int) DT_FCN
<< N_BTSHFT
);
587 /* Keep a list of external functions. */
589 struct GTY(()) extern_list
591 struct extern_list
*next
;
596 static GTY(()) struct extern_list
*extern_head
;
598 /* Assemble an external function reference. We need to keep a list of
599 these, so that we can output the function types at the end of the
600 assembly. We can't output the types now, because we might see a
601 definition of the function later on and emit debugging information
605 i386_pe_record_external_function (tree decl
, const char *name
)
607 struct extern_list
*p
;
609 p
= ggc_alloc
<extern_list
> ();
610 p
->next
= extern_head
;
616 /* Keep a list of exported symbols. */
618 struct GTY(()) export_list
620 struct export_list
*next
;
622 int is_data
; /* used to type tag exported symbols. */
625 /* Keep a list of stub symbols. */
627 struct GTY(()) stub_list
629 struct stub_list
*next
;
633 static GTY(()) struct export_list
*export_head
;
635 static GTY(()) struct stub_list
*stub_head
;
637 /* Assemble an export symbol entry. We need to keep a list of
638 these, so that we can output the export list at the end of the
639 assembly. We used to output these export symbols in each function,
640 but that causes problems with GNU ld when the sections are
641 linkonce. Beware, DECL may be NULL if compile_file() is emitting
645 i386_pe_maybe_record_exported_symbol (tree decl
, const char *name
, int is_data
)
648 struct export_list
*p
;
653 symbol
= XEXP (DECL_RTL (decl
), 0);
654 gcc_assert (GET_CODE (symbol
) == SYMBOL_REF
);
655 if (!SYMBOL_REF_DLLEXPORT_P (symbol
))
658 gcc_assert (TREE_PUBLIC (decl
));
660 p
= ggc_alloc
<export_list
> ();
661 p
->next
= export_head
;
663 p
->is_data
= is_data
;
668 i386_pe_record_stub (const char *name
)
672 if (!name
|| *name
== 0)
678 if (p
->name
[0] == *name
679 && !strcmp (p
->name
, name
))
684 p
= ggc_alloc
<stub_list
> ();
691 #ifdef CXX_WRAP_SPEC_LIST
693 /* Search for a function named TARGET in the list of library wrappers
694 we are using, returning a pointer to it if found or NULL if not.
695 This function might be called on quite a few symbols, and we only
696 have the list of names of wrapped functions available to us as a
697 spec string, so first time round we lazily initialise a hash table
698 to make things quicker. */
701 i386_find_on_wrapper_list (const char *target
)
703 static char first_time
= 1;
704 static hash_table
<nofree_string_hash
> *wrappers
;
708 /* Beware that this is not a complicated parser, it assumes
709 that any sequence of non-whitespace beginning with an
710 underscore is one of the wrapped symbols. For now that's
711 adequate to distinguish symbols from spec substitutions
712 and command-line options. */
713 static char wrapper_list_buffer
[] = CXX_WRAP_SPEC_LIST
;
715 /* Breaks up the char array into separated strings
716 strings and enter them into the hash table. */
717 wrappers
= new hash_table
<nofree_string_hash
> (8);
718 for (bufptr
= wrapper_list_buffer
; *bufptr
; ++bufptr
)
721 if (ISSPACE (*bufptr
))
725 while (*bufptr
&& !ISSPACE (*bufptr
))
730 *wrappers
->find_slot (found
, INSERT
) = found
;
735 return wrappers
->find (target
);
738 #endif /* CXX_WRAP_SPEC_LIST */
740 /* This is called at the end of assembly. For each external function
741 which has not been defined, we output a declaration now. We also
742 output the .drectve section. */
745 i386_pe_file_end (void)
747 struct extern_list
*p
;
749 for (p
= extern_head
; p
!= NULL
; p
= p
->next
)
755 /* Positively ensure only one declaration for any given symbol. */
756 if (! TREE_ASM_WRITTEN (decl
)
757 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
759 #ifdef CXX_WRAP_SPEC_LIST
760 /* To ensure the DLL that provides the corresponding real
761 functions is still loaded at runtime, we must reference
762 the real function so that an (unused) import is created. */
763 const char *realsym
= i386_find_on_wrapper_list (p
->name
);
765 i386_pe_declare_function_type (asm_out_file
,
766 concat ("__real_", realsym
, NULL
), TREE_PUBLIC (decl
));
767 #endif /* CXX_WRAP_SPEC_LIST */
768 TREE_ASM_WRITTEN (decl
) = 1;
769 i386_pe_declare_function_type (asm_out_file
, p
->name
,
776 struct export_list
*q
;
778 for (q
= export_head
; q
!= NULL
; q
= q
->next
)
780 fprintf (asm_out_file
, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
781 default_strip_name_encoding (q
->name
),
782 (q
->is_data
? ",data" : ""));
790 for (q
= stub_head
; q
!= NULL
; q
= q
->next
)
792 const char *name
= q
->name
;
800 if (strncmp (name
, "refptr.", 7) != 0)
803 fprintf (asm_out_file
, "\t.section\t.rdata$%s, \"dr\"\n"
805 "\t.linkonce\tdiscard\n", oname
, oname
);
806 fprintf (asm_out_file
, "%s:\n\t.quad\t%s\n", oname
, name
);
812 /* x64 Structured Exception Handling unwind info. */
814 struct seh_frame_state
816 /* SEH records saves relative to the "current" stack pointer, whether
817 or not there's a frame pointer in place. This tracks the current
818 stack pointer offset from the CFA. */
819 HOST_WIDE_INT sp_offset
;
821 /* The CFA is located at CFA_REG + CFA_OFFSET. */
822 HOST_WIDE_INT cfa_offset
;
825 /* The offset wrt the CFA where register N has been saved. */
826 HOST_WIDE_INT reg_offset
[FIRST_PSEUDO_REGISTER
];
828 /* True if we are past the end of the epilogue. */
831 /* True if we are in the cold section. */
832 bool in_cold_section
;
835 /* Set up data structures beginning output for SEH. */
838 i386_pe_seh_init (FILE *f
)
840 struct seh_frame_state
*seh
;
847 /* We cannot support DRAP with SEH. We turned off support for it by
848 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
849 gcc_assert (!stack_realign_drap
);
851 seh
= XCNEW (struct seh_frame_state
);
852 cfun
->machine
->seh
= seh
;
854 seh
->sp_offset
= INCOMING_FRAME_SP_OFFSET
;
855 seh
->cfa_offset
= INCOMING_FRAME_SP_OFFSET
;
856 seh
->cfa_reg
= stack_pointer_rtx
;
858 fputs ("\t.seh_proc\t", f
);
859 assemble_name (f
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun
->decl
)));
863 /* Emit an assembler directive for the end of the prologue. */
866 i386_pe_seh_end_prologue (FILE *f
)
872 cfun
->machine
->seh
->after_prologue
= true;
873 fputs ("\t.seh_endprologue\n", f
);
876 /* Emit assembler directives to reconstruct the SEH state. */
879 i386_pe_seh_cold_init (FILE *f
, const char *name
)
881 struct seh_frame_state
*seh
;
882 HOST_WIDE_INT alloc_offset
, offset
;
888 seh
= cfun
->machine
->seh
;
890 fputs ("\t.seh_proc\t", f
);
891 assemble_name (f
, name
);
894 /* In the normal case, the frame pointer is near the bottom of the frame
895 so we can do the full stack allocation and set it afterwards. There
896 is an exception when the function accesses prior frames so, in this
897 case, we need to pre-allocate a small chunk before setting it. */
898 if (crtl
->accesses_prior_frames
)
899 alloc_offset
= seh
->cfa_offset
;
901 alloc_offset
= seh
->sp_offset
;
903 offset
= alloc_offset
- INCOMING_FRAME_SP_OFFSET
;
904 if (offset
> 0 && offset
< SEH_MAX_FRAME_SIZE
)
905 fprintf (f
, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC
"\n", offset
);
907 for (int regno
= 0; regno
< FIRST_PSEUDO_REGISTER
; regno
++)
908 if (seh
->reg_offset
[regno
] > 0)
910 fputs ((SSE_REGNO_P (regno
) ? "\t.seh_savexmm\t"
911 : GENERAL_REGNO_P (regno
) ? "\t.seh_savereg\t"
912 : (gcc_unreachable (), "")), f
);
913 print_reg (gen_rtx_REG (DImode
, regno
), 0, f
);
914 fprintf (f
, ", " HOST_WIDE_INT_PRINT_DEC
"\n",
915 alloc_offset
- seh
->reg_offset
[regno
]);
918 if (seh
->cfa_reg
!= stack_pointer_rtx
)
920 offset
= alloc_offset
- seh
->cfa_offset
;
922 gcc_assert ((offset
& 15) == 0);
923 gcc_assert (IN_RANGE (offset
, 0, 240));
925 fputs ("\t.seh_setframe\t", f
);
926 print_reg (seh
->cfa_reg
, 0, f
);
927 fprintf (f
, ", " HOST_WIDE_INT_PRINT_DEC
"\n", offset
);
930 if (crtl
->accesses_prior_frames
)
932 offset
= seh
->sp_offset
- alloc_offset
;
933 if (offset
> 0 && offset
< SEH_MAX_FRAME_SIZE
)
934 fprintf (f
, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC
"\n", offset
);
937 fputs ("\t.seh_endprologue\n", f
);
940 /* Emit an assembler directive for the end of the function. */
943 i386_pe_seh_fini (FILE *f
, bool cold
)
945 struct seh_frame_state
*seh
;
951 seh
= cfun
->machine
->seh
;
952 if (cold
!= seh
->in_cold_section
)
955 cfun
->machine
->seh
= NULL
;
956 fputs ("\t.seh_endproc\n", f
);
959 /* Emit an assembler directive to save REG via a PUSH. */
962 seh_emit_push (FILE *f
, struct seh_frame_state
*seh
, rtx reg
)
964 const unsigned int regno
= REGNO (reg
);
966 gcc_checking_assert (GENERAL_REGNO_P (regno
));
968 seh
->sp_offset
+= UNITS_PER_WORD
;
969 seh
->reg_offset
[regno
] = seh
->sp_offset
;
970 if (seh
->cfa_reg
== stack_pointer_rtx
)
971 seh
->cfa_offset
+= UNITS_PER_WORD
;
973 fputs ("\t.seh_pushreg\t", f
);
974 print_reg (reg
, 0, f
);
978 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
981 seh_emit_save (FILE *f
, struct seh_frame_state
*seh
,
982 rtx reg
, HOST_WIDE_INT cfa_offset
)
984 const unsigned int regno
= REGNO (reg
);
985 HOST_WIDE_INT offset
;
987 seh
->reg_offset
[regno
] = cfa_offset
;
989 /* Negative save offsets are of course not supported, since that
990 would be a store below the stack pointer and thus clobberable. */
991 gcc_assert (seh
->sp_offset
>= cfa_offset
);
992 offset
= seh
->sp_offset
- cfa_offset
;
994 fputs ((SSE_REGNO_P (regno
) ? "\t.seh_savexmm\t"
995 : GENERAL_REGNO_P (regno
) ? "\t.seh_savereg\t"
996 : (gcc_unreachable (), "")), f
);
997 print_reg (reg
, 0, f
);
998 fprintf (f
, ", " HOST_WIDE_INT_PRINT_DEC
"\n", offset
);
1001 /* Emit an assembler directive to adjust RSP by OFFSET. */
1004 seh_emit_stackalloc (FILE *f
, struct seh_frame_state
*seh
,
1005 HOST_WIDE_INT offset
)
1007 /* We're only concerned with prologue stack allocations, which all
1008 are subtractions from the stack pointer. */
1009 gcc_assert (offset
< 0);
1012 if (seh
->cfa_reg
== stack_pointer_rtx
)
1013 seh
->cfa_offset
+= offset
;
1014 seh
->sp_offset
+= offset
;
1016 /* Do not output the stackalloc in that case (it won't work as there is no
1017 encoding for very large frame size). */
1018 if (offset
< SEH_MAX_FRAME_SIZE
)
1019 fprintf (f
, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC
"\n", offset
);
1022 /* Process REG_CFA_ADJUST_CFA for SEH. */
1025 seh_cfa_adjust_cfa (FILE *f
, struct seh_frame_state
*seh
, rtx pat
)
1028 HOST_WIDE_INT reg_offset
= 0;
1029 unsigned int dest_regno
;
1031 dest
= SET_DEST (pat
);
1032 src
= SET_SRC (pat
);
1034 if (GET_CODE (src
) == PLUS
)
1036 reg_offset
= INTVAL (XEXP (src
, 1));
1037 src
= XEXP (src
, 0);
1039 else if (GET_CODE (src
) == MINUS
)
1041 reg_offset
= -INTVAL (XEXP (src
, 1));
1042 src
= XEXP (src
, 0);
1044 gcc_assert (src
== stack_pointer_rtx
);
1045 gcc_assert (seh
->cfa_reg
== stack_pointer_rtx
);
1046 dest_regno
= REGNO (dest
);
1048 if (dest_regno
== STACK_POINTER_REGNUM
)
1049 seh_emit_stackalloc (f
, seh
, reg_offset
);
1050 else if (dest_regno
== HARD_FRAME_POINTER_REGNUM
)
1052 HOST_WIDE_INT offset
;
1054 seh
->cfa_reg
= dest
;
1055 seh
->cfa_offset
-= reg_offset
;
1057 offset
= seh
->sp_offset
- seh
->cfa_offset
;
1059 gcc_assert ((offset
& 15) == 0);
1060 gcc_assert (IN_RANGE (offset
, 0, 240));
1062 fputs ("\t.seh_setframe\t", f
);
1063 print_reg (seh
->cfa_reg
, 0, f
);
1064 fprintf (f
, ", " HOST_WIDE_INT_PRINT_DEC
"\n", offset
);
1070 /* Process REG_CFA_OFFSET for SEH. */
1073 seh_cfa_offset (FILE *f
, struct seh_frame_state
*seh
, rtx pat
)
1076 HOST_WIDE_INT reg_offset
;
1078 dest
= SET_DEST (pat
);
1079 src
= SET_SRC (pat
);
1081 gcc_assert (MEM_P (dest
));
1082 dest
= XEXP (dest
, 0);
1087 gcc_assert (GET_CODE (dest
) == PLUS
);
1088 reg_offset
= INTVAL (XEXP (dest
, 1));
1089 dest
= XEXP (dest
, 0);
1091 gcc_assert (dest
== seh
->cfa_reg
);
1093 seh_emit_save (f
, seh
, src
, seh
->cfa_offset
- reg_offset
);
1096 /* Process a FRAME_RELATED_EXPR for SEH. */
1099 seh_frame_related_expr (FILE *f
, struct seh_frame_state
*seh
, rtx pat
)
1102 HOST_WIDE_INT addend
;
1104 /* See the full loop in dwarf2out_frame_debug_expr. */
1105 if (GET_CODE (pat
) == PARALLEL
|| GET_CODE (pat
) == SEQUENCE
)
1107 int i
, n
= XVECLEN (pat
, 0), pass
, npass
;
1109 npass
= (GET_CODE (pat
) == PARALLEL
? 2 : 1);
1110 for (pass
= 0; pass
< npass
; ++pass
)
1111 for (i
= 0; i
< n
; ++i
)
1113 rtx ele
= XVECEXP (pat
, 0, i
);
1115 if (GET_CODE (ele
) != SET
)
1117 dest
= SET_DEST (ele
);
1119 /* Process each member of the PARALLEL independently. The first
1120 member is always processed; others only if they are marked. */
1121 if (i
== 0 || RTX_FRAME_RELATED_P (ele
))
1123 /* Evaluate all register saves in the first pass and all
1124 register updates in the second pass. */
1125 if ((MEM_P (dest
) ^ pass
) || npass
== 1)
1126 seh_frame_related_expr (f
, seh
, ele
);
1132 dest
= SET_DEST (pat
);
1133 src
= SET_SRC (pat
);
1135 switch (GET_CODE (dest
))
1138 switch (GET_CODE (src
))
1141 /* REG = REG: This should be establishing a frame pointer. */
1142 gcc_assert (src
== stack_pointer_rtx
);
1143 gcc_assert (dest
== hard_frame_pointer_rtx
);
1144 seh_cfa_adjust_cfa (f
, seh
, pat
);
1148 addend
= INTVAL (XEXP (src
, 1));
1149 src
= XEXP (src
, 0);
1150 if (dest
== hard_frame_pointer_rtx
)
1151 seh_cfa_adjust_cfa (f
, seh
, pat
);
1152 else if (dest
== stack_pointer_rtx
)
1154 gcc_assert (src
== stack_pointer_rtx
);
1155 seh_emit_stackalloc (f
, seh
, addend
);
1167 /* A save of some kind. */
1168 dest
= XEXP (dest
, 0);
1169 if (GET_CODE (dest
) == PRE_DEC
)
1171 gcc_checking_assert (GET_MODE (src
) == Pmode
);
1172 gcc_checking_assert (REG_P (src
));
1173 seh_emit_push (f
, seh
, src
);
1176 seh_cfa_offset (f
, seh
, pat
);
1184 /* This function looks at a single insn and emits any SEH directives
1185 required for unwind of this insn. */
1188 i386_pe_seh_unwind_emit (FILE *asm_out_file
, rtx_insn
*insn
)
1191 bool handled_one
= false;
1192 struct seh_frame_state
*seh
;
1197 seh
= cfun
->machine
->seh
;
1198 if (NOTE_P (insn
) && NOTE_KIND (insn
) == NOTE_INSN_SWITCH_TEXT_SECTIONS
)
1200 fputs ("\t.seh_endproc\n", asm_out_file
);
1201 seh
->in_cold_section
= true;
1205 if (NOTE_P (insn
) || !RTX_FRAME_RELATED_P (insn
))
1208 /* Skip RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1209 if (seh
->after_prologue
)
1212 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
1214 switch (REG_NOTE_KIND (note
))
1216 case REG_FRAME_RELATED_EXPR
:
1217 pat
= XEXP (note
, 0);
1220 case REG_CFA_DEF_CFA
:
1221 case REG_CFA_EXPRESSION
:
1222 /* Only emitted with DRAP and aligned memory access using a
1223 realigned SP, both of which we disable. */
1227 case REG_CFA_REGISTER
:
1228 /* Only emitted in epilogues, which we skip. */
1231 case REG_CFA_ADJUST_CFA
:
1232 pat
= XEXP (note
, 0);
1235 pat
= PATTERN (insn
);
1236 if (GET_CODE (pat
) == PARALLEL
)
1237 pat
= XVECEXP (pat
, 0, 0);
1239 seh_cfa_adjust_cfa (asm_out_file
, seh
, pat
);
1243 case REG_CFA_OFFSET
:
1244 pat
= XEXP (note
, 0);
1246 pat
= single_set (insn
);
1247 seh_cfa_offset (asm_out_file
, seh
, pat
);
1257 pat
= PATTERN (insn
);
1259 seh_frame_related_expr (asm_out_file
, seh
, pat
);
1263 i386_pe_seh_emit_except_personality (rtx personality
)
1270 fputs ("\t.seh_handler\t", asm_out_file
);
1271 output_addr_const (asm_out_file
, personality
);
1274 /* ??? The current implementation of _GCC_specific_handler requires
1275 both except and unwind handling, regardless of which sorts the
1276 user-level function requires. */
1278 FOR_ALL_EH_REGION(r
)
1280 if (r
->type
== ERT_CLEANUP
)
1290 fputs (", @unwind", asm_out_file
);
1292 fputs (", @except", asm_out_file
);
1293 fputc ('\n', asm_out_file
);
1297 i386_pe_seh_init_sections (void)
1300 exception_section
= get_unnamed_section (0, output_section_asm_op
,
1301 "\t.seh_handlerdata");
1305 i386_pe_start_function (FILE *f
, const char *name
, tree decl
)
1307 i386_pe_maybe_record_exported_symbol (decl
, name
, 0);
1308 i386_pe_declare_function_type (f
, name
, TREE_PUBLIC (decl
));
1309 /* In case section was altered by debugging output. */
1310 if (decl
!= NULL_TREE
)
1311 switch_to_section (function_section (decl
));
1312 ASM_OUTPUT_FUNCTION_LABEL (f
, name
, decl
);
1316 i386_pe_end_function (FILE *f
, const char *, tree
)
1318 i386_pe_seh_fini (f
, false);
1322 i386_pe_end_cold_function (FILE *f
, const char *, tree
)
1324 i386_pe_seh_fini (f
, true);
1327 #include "gt-winnt.h"