1 /* Subroutines for insn-output.c for Windows NT.
2 Contributed by Douglas Rupp (drupp@cs.washington.edu)
3 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 #include "hard-reg-set.h"
33 #include "diagnostic-core.h"
35 #include "langhooks.h"
39 #include "lto-streamer.h"
41 /* i386/PE specific attribute support.
43 i386/PE has two new attributes:
44 dllexport - for exporting a function/variable that will live in a dll
45 dllimport - for importing a function/variable from a dll
47 Microsoft allows multiple declspecs in one __declspec, separating
48 them with spaces. We do NOT support this. Instead, use __declspec
52 /* Handle a "shared" attribute;
53 arguments as in struct attribute_spec.handler. */
55 ix86_handle_shared_attribute (tree *node, tree name,
56 tree args ATTRIBUTE_UNUSED,
57 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
59 if (TREE_CODE (*node) != VAR_DECL)
61 warning (OPT_Wattributes, "%qE attribute only applies to variables",
69 /* Handle a "selectany" attribute;
70 arguments as in struct attribute_spec.handler. */
72 ix86_handle_selectany_attribute (tree *node, tree name,
73 tree args ATTRIBUTE_UNUSED,
74 int flags ATTRIBUTE_UNUSED,
77 /* The attribute applies only to objects that are initialized and have
78 external linkage. However, we may not know about initialization
79 until the language frontend has processed the decl. We'll check for
80 initialization later in encode_section_info. */
81 if (TREE_CODE (*node) != VAR_DECL || !TREE_PUBLIC (*node))
83 error ("%qE attribute applies only to initialized variables"
84 " with external linkage", name);
92 /* Return the type that we should use to determine if DECL is
93 imported or exported. */
96 associated_type (tree decl)
98 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
99 ? DECL_CONTEXT (decl) : NULL_TREE);
102 /* Return true if DECL should be a dllexport'd object. */
105 i386_pe_determine_dllexport_p (tree decl)
107 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
110 /* Don't export local clones of dllexports. */
111 if (!TREE_PUBLIC (decl))
114 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
120 /* Return true if DECL should be a dllimport'd object. */
123 i386_pe_determine_dllimport_p (tree decl)
127 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
130 if (DECL_DLLIMPORT_P (decl))
133 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
134 by targetm.cxx.adjust_class_at_definition. Check again to emit
135 error message if the class attribute has been overridden by an
136 out-of-class definition of static data. */
137 assoc = associated_type (decl);
138 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc))
139 && TREE_CODE (decl) == VAR_DECL
140 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
141 && !DECL_EXTERNAL (decl)
142 /* vtable's are linkonce constants, so defining a vtable is not
143 an error as long as we don't try to import it too. */
144 && !DECL_VIRTUAL_P (decl))
145 error ("definition of static data member %q+D of "
146 "dllimport%'d class", decl);
151 /* Handle the -mno-fun-dllimport target switch. */
154 i386_pe_valid_dllimport_attribute_p (const_tree decl)
156 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
161 /* Return string which is the function name, identified by ID, modified
162 with a suffix consisting of an atsign (@) followed by the number of
163 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
164 FASTCALL is true, also add the FASTCALL_PREFIX.
165 Return NULL if no change required. */
168 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
170 HOST_WIDE_INT total = 0;
171 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
173 tree type = TREE_TYPE (DECL_ORIGIN (decl));
175 function_args_iterator args_iter;
177 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
179 if (prototype_p (type))
181 /* This attribute is ignored for variadic functions. */
185 /* Quit if we hit an incomplete type. Error is reported
186 by convert_arguments in c-typeck.c or cp/typeck.c. */
187 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
189 HOST_WIDE_INT parm_size;
190 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
192 if (! COMPLETE_TYPE_P (arg))
195 parm_size = int_size_in_bytes (arg);
199 /* Must round up to include padding. This is done the same
200 way as in store_one_arg. */
201 parm_size = ((parm_size + parm_boundary_bytes - 1)
202 / parm_boundary_bytes * parm_boundary_bytes);
207 /* Assume max of 8 base 10 digits in the suffix. */
208 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
210 *p++ = FASTCALL_PREFIX;
211 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
213 return get_identifier (new_str);
216 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
217 fastcall function. The original identifier is supplied in ID. */
220 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
222 tree new_id = NULL_TREE;
224 if (TREE_CODE (decl) == FUNCTION_DECL)
226 unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
227 if ((ccvt & IX86_CALLCVT_STDCALL) != 0)
230 /* If we are using -mrtd emit undecorated symbol and let linker
231 do the proper resolving. */
233 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
235 else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
236 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
242 /* Emit an assembler directive to set symbol for DECL visibility to
243 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
244 As for PE there is no hidden support in gas, we just warn for
245 user-specified visibility attributes. */
248 i386_pe_assemble_visibility (tree decl,
249 int vis ATTRIBUTE_UNUSED)
252 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
254 warning (OPT_Wattributes, "visibility attribute not supported "
255 "in this configuration; ignored");
258 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
259 in the language-independent default hook
260 langhooks,c:lhd_set_decl_assembler_name ()
261 and in cp/mangle,c:mangle_decl (). */
263 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
265 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
267 return (new_id ? new_id : id);
270 /* This hook behaves the same as varasm.c/assemble_name(), but
271 generates the name into memory rather than outputting it to
275 i386_pe_mangle_assembler_name (const char *name ATTRIBUTE_UNUSED)
277 const char *skipped = name + (*name == '*' ? 1 : 0);
278 const char *stripped = targetm.strip_name_encoding (skipped);
279 if (*name != '*' && *user_label_prefix && *stripped != FASTCALL_PREFIX)
280 stripped = ACONCAT ((user_label_prefix, stripped, NULL));
281 return get_identifier (stripped);
285 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
290 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
291 default_encode_section_info (decl, rtl, first);
293 /* Careful not to prod global register variables. */
297 symbol = XEXP (rtl, 0);
298 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
300 switch (TREE_CODE (decl))
303 /* FIXME: Imported stdcall names are not modified by the Ada frontend.
304 Check and decorate the RTL name now. */
305 if (strcmp (lang_hooks.name, "GNU Ada") == 0)
308 tree old_id = DECL_ASSEMBLER_NAME (decl);
309 const char* asm_str = IDENTIFIER_POINTER (old_id);
310 /* Do not change the identifier if a verbatim asmspec
311 or if stdcall suffix already added. */
312 if (!(*asm_str == '*' || strchr (asm_str, '@'))
313 && (new_id = i386_pe_maybe_mangle_decl_assembler_name (decl,
315 XSTR (symbol, 0) = IDENTIFIER_POINTER (new_id);
320 if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
322 if (DECL_INITIAL (decl)
323 /* If an object is initialized with a ctor, the static
324 initialization and destruction code for it is present in
325 each unit defining the object. The code that calls the
326 ctor is protected by a link-once guard variable, so that
327 the object still has link-once semantics, */
328 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
329 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
331 error ("%q+D:'selectany' attribute applies only to "
332 "initialized objects", decl);
340 /* Mark the decl so we can tell from the rtl whether the object is
341 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
342 handles dllexport/dllimport override semantics. */
343 flags = (SYMBOL_REF_FLAGS (symbol) &
344 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
345 if (i386_pe_determine_dllexport_p (decl))
346 flags |= SYMBOL_FLAG_DLLEXPORT;
347 else if (i386_pe_determine_dllimport_p (decl))
348 flags |= SYMBOL_FLAG_DLLIMPORT;
350 SYMBOL_REF_FLAGS (symbol) = flags;
354 i386_pe_binds_local_p (const_tree exp)
356 /* PE does not do dynamic binding. Indeed, the only kind of
357 non-local reference comes from a dllimport'd symbol. */
358 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
359 && DECL_DLLIMPORT_P (exp))
362 /* Or a weak one, now that they are supported. */
363 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
370 /* Also strip the fastcall prefix and stdcall suffix. */
373 i386_pe_strip_name_encoding_full (const char *str)
376 const char *name = default_strip_name_encoding (str);
378 /* Strip leading '@' on fastcall symbols. */
382 /* Strip trailing "@n". */
383 p = strchr (name, '@');
385 return ggc_alloc_string (name, p - name);
391 i386_pe_unique_section (tree decl, int reloc)
394 const char *name, *prefix;
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 DECL_SECTION_NAME (decl) = build_string (len, string);
419 /* Select a set of attributes for section NAME based on the properties
420 of DECL and whether or not RELOC indicates that DECL's initializer
421 might contain runtime relocations.
423 We make the section read-only and executable for a function decl,
424 read-only for a const data decl, and writable for a non-const data decl.
426 If the section has already been defined, to not allow it to have
427 different attributes, as (1) this is ambiguous since we're not seeing
428 all the declarations up front and (2) some assemblers (e.g. SVR4)
429 do not recognize section redefinitions. */
430 /* ??? This differs from the "standard" PE implementation in that we
431 handle the SHARED variable attribute. Should this be done for all
434 #define SECTION_PE_SHARED SECTION_MACH_DEP
437 i386_pe_section_type_flags (tree decl, const char *name, int reloc)
443 /* The names we put in the hashtable will always be the unique
444 versions given to us by the stringtable, so we can just use
445 their addresses as the keys. */
447 htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
449 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
450 flags = SECTION_CODE;
451 else if (decl && decl_readonly_section (decl, reloc))
455 flags = SECTION_WRITE;
457 if (decl && TREE_CODE (decl) == VAR_DECL
458 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
459 flags |= SECTION_PE_SHARED;
462 if (decl && DECL_ONE_ONLY (decl))
463 flags |= SECTION_LINKONCE;
465 /* See if we already have an entry for this section. */
466 slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
469 *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
474 if (decl && **slot != flags)
475 error ("%q+D causes a section type conflict", decl);
482 i386_pe_asm_named_section (const char *name, unsigned int flags,
485 char flagchars[8], *f = flagchars;
487 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
490 *f++ ='d'; /* This is necessary for older versions of gas. */
495 if (flags & SECTION_CODE)
497 if (flags & SECTION_WRITE)
499 if (flags & SECTION_PE_SHARED)
503 /* LTO sections need 1-byte alignment to avoid confusing the
504 zlib decompression algorithm with trailing zero pad bytes. */
505 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
506 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
511 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
513 if (flags & SECTION_LINKONCE)
515 /* Functions may have been compiled at various levels of
516 optimization so we can't use `same_size' here.
517 Instead, have the linker pick one, without warning.
518 If 'selectany' attribute has been specified, MS compiler
519 sets 'discard' characteristic, rather than telling linker
520 to warn of size or content mismatch, so do the same. */
521 bool discard = (flags & SECTION_CODE)
522 || lookup_attribute ("selectany",
523 DECL_ATTRIBUTES (decl));
524 fprintf (asm_out_file, "\t.linkonce %s\n",
525 (discard ? "discard" : "same_size"));
529 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
532 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
533 const char *name, HOST_WIDE_INT size,
534 HOST_WIDE_INT align ATTRIBUTE_UNUSED)
536 HOST_WIDE_INT rounded;
538 /* Compute as in assemble_noswitch_variable, since we don't have
539 support for aligned common on older binutils. We must also
540 avoid emitting a common symbol of size zero, as this is the
541 overloaded representation that indicates an undefined external
542 symbol in the PE object file format. */
543 rounded = size ? size : 1;
544 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
545 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
546 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
548 i386_pe_maybe_record_exported_symbol (decl, name, 1);
550 fprintf (stream, "\t.comm\t");
551 assemble_name (stream, name);
552 if (use_pe_aligned_common)
553 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
554 size ? size : (HOST_WIDE_INT) 1,
555 exact_log2 (align) - exact_log2 (CHAR_BIT));
557 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
558 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
561 /* The Microsoft linker requires that every function be marked as
562 DT_FCN. When using gas on cygwin, we must emit appropriate .type
567 /* Mark a function appropriately. This should only be called for
568 functions for which we are not emitting COFF debugging information.
569 FILE is the assembler output file, NAME is the name of the
570 function, and PUB is nonzero if the function is globally
574 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
576 fprintf (file, "\t.def\t");
577 assemble_name (file, name);
578 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
579 pub ? (int) C_EXT : (int) C_STAT,
580 (int) DT_FCN << N_BTSHFT);
583 /* Keep a list of external functions. */
585 struct GTY(()) extern_list
587 struct extern_list *next;
592 static GTY(()) struct extern_list *extern_head;
594 /* Assemble an external function reference. We need to keep a list of
595 these, so that we can output the function types at the end of the
596 assembly. We can't output the types now, because we might see a
597 definition of the function later on and emit debugging information
601 i386_pe_record_external_function (tree decl, const char *name)
603 struct extern_list *p;
605 p = ggc_alloc_extern_list ();
606 p->next = extern_head;
612 /* Keep a list of exported symbols. */
614 struct GTY(()) export_list
616 struct export_list *next;
618 int is_data; /* used to type tag exported symbols. */
621 static GTY(()) struct export_list *export_head;
623 /* Assemble an export symbol entry. We need to keep a list of
624 these, so that we can output the export list at the end of the
625 assembly. We used to output these export symbols in each function,
626 but that causes problems with GNU ld when the sections are
627 linkonce. Beware, DECL may be NULL if compile_file() is emitting
631 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
634 struct export_list *p;
639 symbol = XEXP (DECL_RTL (decl), 0);
640 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
641 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
644 gcc_assert (TREE_PUBLIC (decl));
646 p = ggc_alloc_export_list ();
647 p->next = export_head;
649 p->is_data = is_data;
653 #ifdef CXX_WRAP_SPEC_LIST
655 /* Hash table equality helper function. */
658 wrapper_strcmp (const void *x, const void *y)
660 return !strcmp ((const char *) x, (const char *) y);
663 /* Search for a function named TARGET in the list of library wrappers
664 we are using, returning a pointer to it if found or NULL if not.
665 This function might be called on quite a few symbols, and we only
666 have the list of names of wrapped functions available to us as a
667 spec string, so first time round we lazily initialise a hash table
668 to make things quicker. */
671 i386_find_on_wrapper_list (const char *target)
673 static char first_time = 1;
674 static htab_t wrappers;
678 /* Beware that this is not a complicated parser, it assumes
679 that any sequence of non-whitespace beginning with an
680 underscore is one of the wrapped symbols. For now that's
681 adequate to distinguish symbols from spec substitutions
682 and command-line options. */
683 static char wrapper_list_buffer[] = CXX_WRAP_SPEC_LIST;
685 /* Breaks up the char array into separated strings
686 strings and enter them into the hash table. */
687 wrappers = htab_create_alloc (8, htab_hash_string, wrapper_strcmp,
689 for (bufptr = wrapper_list_buffer; *bufptr; ++bufptr)
692 if (ISSPACE (*bufptr))
696 while (*bufptr && !ISSPACE (*bufptr))
701 *htab_find_slot (wrappers, found, INSERT) = found;
706 return (const char *) htab_find (wrappers, target);
709 #endif /* CXX_WRAP_SPEC_LIST */
711 /* This is called at the end of assembly. For each external function
712 which has not been defined, we output a declaration now. We also
713 output the .drectve section. */
716 i386_pe_file_end (void)
718 struct extern_list *p;
720 for (p = extern_head; p != NULL; p = p->next)
726 /* Positively ensure only one declaration for any given symbol. */
727 if (! TREE_ASM_WRITTEN (decl)
728 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
730 #ifdef CXX_WRAP_SPEC_LIST
731 /* To ensure the DLL that provides the corresponding real
732 functions is still loaded at runtime, we must reference
733 the real function so that an (unused) import is created. */
734 const char *realsym = i386_find_on_wrapper_list (p->name);
736 i386_pe_declare_function_type (asm_out_file,
737 concat ("__real_", realsym, NULL), TREE_PUBLIC (decl));
738 #endif /* CXX_WRAP_SPEC_LIST */
739 TREE_ASM_WRITTEN (decl) = 1;
740 i386_pe_declare_function_type (asm_out_file, p->name,
747 struct export_list *q;
749 for (q = export_head; q != NULL; q = q->next)
751 fprintf (asm_out_file, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
752 default_strip_name_encoding (q->name),
753 (q->is_data ? ",data" : ""));
759 /* x64 Structured Exception Handling unwind info. */
761 struct seh_frame_state
763 /* SEH records saves relative to the "current" stack pointer, whether
764 or not there's a frame pointer in place. This tracks the current
765 stack pointer offset from the CFA. */
766 HOST_WIDE_INT sp_offset;
768 /* The CFA is located at CFA_REG + CFA_OFFSET. */
769 HOST_WIDE_INT cfa_offset;
773 /* Set up data structures beginning output for SEH. */
776 i386_pe_seh_init (FILE *f)
778 struct seh_frame_state *seh;
785 /* We cannot support DRAP with SEH. We turned off support for it by
786 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
787 gcc_assert (!stack_realign_drap);
789 seh = XCNEW (struct seh_frame_state);
790 cfun->machine->seh = seh;
792 seh->sp_offset = INCOMING_FRAME_SP_OFFSET;
793 seh->cfa_offset = INCOMING_FRAME_SP_OFFSET;
794 seh->cfa_reg = stack_pointer_rtx;
796 fputs ("\t.seh_proc\t", f);
797 assemble_name (f, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun->decl)));
802 i386_pe_seh_end_prologue (FILE *f)
804 struct seh_frame_state *seh;
810 seh = cfun->machine->seh;
812 /* Emit an assembler directive to set up the frame pointer. Always do
813 this last. The documentation talks about doing this "before" any
814 other code that uses offsets, but (experimentally) that's after we
815 emit the codes in reverse order (handled by the assembler). */
816 if (seh->cfa_reg != stack_pointer_rtx)
818 HOST_WIDE_INT offset = seh->sp_offset - seh->cfa_offset;
820 gcc_assert ((offset & 15) == 0);
821 gcc_assert (IN_RANGE (offset, 0, 240));
823 fputs ("\t.seh_setframe\t", f);
824 print_reg (seh->cfa_reg, 0, f);
825 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
829 cfun->machine->seh = NULL;
831 fputs ("\t.seh_endprologue\n", f);
835 i386_pe_seh_fini (FILE *f)
841 fputs ("\t.seh_endproc\n", f);
844 /* Emit an assembler directive to save REG via a PUSH. */
847 seh_emit_push (FILE *f, struct seh_frame_state *seh, rtx reg)
849 unsigned int regno = REGNO (reg);
851 gcc_checking_assert (GENERAL_REGNO_P (regno));
853 seh->sp_offset += UNITS_PER_WORD;
854 if (seh->cfa_reg == stack_pointer_rtx)
855 seh->cfa_offset += UNITS_PER_WORD;
857 fputs ("\t.seh_pushreg\t", f);
858 print_reg (reg, 0, f);
862 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
865 seh_emit_save (FILE *f, struct seh_frame_state *seh,
866 rtx reg, HOST_WIDE_INT cfa_offset)
868 unsigned int regno = REGNO (reg);
869 HOST_WIDE_INT offset;
871 /* Negative save offsets are of course not supported, since that
872 would be a store below the stack pointer and thus clobberable. */
873 gcc_assert (seh->sp_offset >= cfa_offset);
874 offset = seh->sp_offset - cfa_offset;
876 fputs ((SSE_REGNO_P (regno) ? "\t.seh_savexmm\t"
877 : GENERAL_REGNO_P (regno) ? "\t.seh_savereg\t"
878 : (gcc_unreachable (), "")), f);
879 print_reg (reg, 0, f);
880 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
883 /* Emit an assembler directive to adjust RSP by OFFSET. */
886 seh_emit_stackalloc (FILE *f, struct seh_frame_state *seh,
887 HOST_WIDE_INT offset)
889 /* We're only concerned with prologue stack allocations, which all
890 are subtractions from the stack pointer. */
891 gcc_assert (offset < 0);
894 if (seh->cfa_reg == stack_pointer_rtx)
895 seh->cfa_offset += offset;
896 seh->sp_offset += offset;
898 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
901 /* Process REG_CFA_ADJUST_CFA for SEH. */
904 seh_cfa_adjust_cfa (FILE *f, struct seh_frame_state *seh, rtx pat)
907 HOST_WIDE_INT reg_offset = 0;
908 unsigned int dest_regno;
910 dest = SET_DEST (pat);
913 if (GET_CODE (src) == PLUS)
915 reg_offset = INTVAL (XEXP (src, 1));
918 else if (GET_CODE (src) == MINUS)
920 reg_offset = -INTVAL (XEXP (src, 1));
923 gcc_assert (src == stack_pointer_rtx);
924 gcc_assert (seh->cfa_reg == stack_pointer_rtx);
925 dest_regno = REGNO (dest);
927 if (dest_regno == STACK_POINTER_REGNUM)
928 seh_emit_stackalloc (f, seh, reg_offset);
929 else if (dest_regno == HARD_FRAME_POINTER_REGNUM)
932 seh->cfa_offset -= reg_offset;
938 /* Process REG_CFA_OFFSET for SEH. */
941 seh_cfa_offset (FILE *f, struct seh_frame_state *seh, rtx pat)
944 HOST_WIDE_INT reg_offset;
946 dest = SET_DEST (pat);
949 gcc_assert (MEM_P (dest));
950 dest = XEXP (dest, 0);
955 gcc_assert (GET_CODE (dest) == PLUS);
956 reg_offset = INTVAL (XEXP (dest, 1));
957 dest = XEXP (dest, 0);
959 gcc_assert (dest == seh->cfa_reg);
961 seh_emit_save (f, seh, src, seh->cfa_offset - reg_offset);
964 /* Process a FRAME_RELATED_EXPR for SEH. */
967 seh_frame_related_expr (FILE *f, struct seh_frame_state *seh, rtx pat)
970 HOST_WIDE_INT addend;
972 /* See the full loop in dwarf2out_frame_debug_expr. */
973 if (GET_CODE (pat) == PARALLEL || GET_CODE (pat) == SEQUENCE)
975 int i, n = XVECLEN (pat, 0), pass, npass;
977 npass = (GET_CODE (pat) == PARALLEL ? 2 : 1);
978 for (pass = 0; pass < npass; ++pass)
979 for (i = 0; i < n; ++i)
981 rtx ele = XVECEXP (pat, 0, i);
983 if (GET_CODE (ele) != SET)
985 dest = SET_DEST (ele);
987 /* Process each member of the PARALLEL independently. The first
988 member is always processed; others only if they are marked. */
989 if (i == 0 || RTX_FRAME_RELATED_P (ele))
991 /* Evaluate all register saves in the first pass and all
992 register updates in the second pass. */
993 if ((MEM_P (dest) ^ pass) || npass == 1)
994 seh_frame_related_expr (f, seh, ele);
1000 dest = SET_DEST (pat);
1001 src = SET_SRC (pat);
1003 switch (GET_CODE (dest))
1006 switch (GET_CODE (src))
1009 /* REG = REG: This should be establishing a frame pointer. */
1010 gcc_assert (src == stack_pointer_rtx);
1011 gcc_assert (dest == hard_frame_pointer_rtx);
1012 seh_cfa_adjust_cfa (f, seh, pat);
1016 addend = INTVAL (XEXP (src, 1));
1017 src = XEXP (src, 0);
1018 if (dest == hard_frame_pointer_rtx)
1019 seh_cfa_adjust_cfa (f, seh, pat);
1020 else if (dest == stack_pointer_rtx)
1022 gcc_assert (src == stack_pointer_rtx);
1023 seh_emit_stackalloc (f, seh, addend);
1035 /* A save of some kind. */
1036 dest = XEXP (dest, 0);
1037 if (GET_CODE (dest) == PRE_DEC)
1039 gcc_checking_assert (GET_MODE (src) == Pmode);
1040 gcc_checking_assert (REG_P (src));
1041 seh_emit_push (f, seh, src);
1044 seh_cfa_offset (f, seh, pat);
1052 /* This function looks at a single insn and emits any SEH directives
1053 required for unwind of this insn. */
1056 i386_pe_seh_unwind_emit (FILE *asm_out_file, rtx insn)
1059 bool handled_one = false;
1060 struct seh_frame_state *seh;
1065 /* We free the SEH data once done with the prologue. Ignore those
1066 RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1067 seh = cfun->machine->seh;
1071 if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
1074 for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
1076 pat = XEXP (note, 0);
1077 switch (REG_NOTE_KIND (note))
1079 case REG_FRAME_RELATED_EXPR:
1082 case REG_CFA_DEF_CFA:
1083 case REG_CFA_EXPRESSION:
1084 /* Only emitted with DRAP, which we disable. */
1088 case REG_CFA_REGISTER:
1089 /* Only emitted in epilogues, which we skip. */
1092 case REG_CFA_ADJUST_CFA:
1095 pat = PATTERN (insn);
1096 if (GET_CODE (pat) == PARALLEL)
1097 pat = XVECEXP (pat, 0, 0);
1099 seh_cfa_adjust_cfa (asm_out_file, seh, pat);
1103 case REG_CFA_OFFSET:
1105 pat = single_set (insn);
1106 seh_cfa_offset (asm_out_file, seh, pat);
1116 pat = PATTERN (insn);
1118 seh_frame_related_expr (asm_out_file, seh, pat);
1122 i386_pe_start_function (FILE *f, const char *name, tree decl)
1124 i386_pe_maybe_record_exported_symbol (decl, name, 0);
1125 if (write_symbols != SDB_DEBUG)
1126 i386_pe_declare_function_type (f, name, TREE_PUBLIC (decl));
1127 /* In case section was altered by debugging output. */
1128 if (decl != NULL_TREE)
1129 switch_to_section (function_section (decl));
1130 ASM_OUTPUT_FUNCTION_LABEL (f, name, decl);
1134 i386_pe_end_function (FILE *f, const char *name ATTRIBUTE_UNUSED,
1135 tree decl ATTRIBUTE_UNUSED)
1137 i386_pe_seh_fini (f);
1141 #include "gt-winnt.h"