* i386-protos.h (x86_64_sign_extended_value): Fix prototype.
[official-gcc.git] / gcc / config / i386 / winnt.c
blobf6417aa30bff7586773492b32802f69a3a15715d
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
4 Free Software Foundation, Inc.
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "output.h"
29 #include "tree.h"
30 #include "flags.h"
31 #include "tm_p.h"
32 #include "toplev.h"
33 #include "hashtab.h"
34 #include "ggc.h"
36 /* i386/PE specific attribute support.
38 i386/PE has two new attributes:
39 dllexport - for exporting a function/variable that will live in a dll
40 dllimport - for importing a function/variable from a dll
42 Microsoft allows multiple declspecs in one __declspec, separating
43 them with spaces. We do NOT support this. Instead, use __declspec
44 multiple times.
47 static tree associated_type PARAMS ((tree));
48 const char * gen_stdcall_suffix PARAMS ((tree));
49 int i386_pe_dllexport_p PARAMS ((tree));
50 int i386_pe_dllimport_p PARAMS ((tree));
51 void i386_pe_mark_dllexport PARAMS ((tree));
52 void i386_pe_mark_dllimport PARAMS ((tree));
54 /* Handle a "dllimport" or "dllexport" attribute;
55 arguments as in struct attribute_spec.handler. */
56 tree
57 ix86_handle_dll_attribute (node, name, args, flags, no_add_attrs)
58 tree *node;
59 tree name;
60 tree args;
61 int flags;
62 bool *no_add_attrs;
64 /* These attributes may apply to structure and union types being created,
65 but otherwise should pass to the declaration involved. */
66 if (!DECL_P (*node))
68 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
69 | (int) ATTR_FLAG_ARRAY_NEXT))
71 *no_add_attrs = true;
72 return tree_cons (name, args, NULL_TREE);
74 if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
76 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
77 *no_add_attrs = true;
81 /* `extern' needn't be specified with dllimport.
82 Specify `extern' now and hope for the best. Sigh. */
83 else if (TREE_CODE (*node) == VAR_DECL
84 && is_attribute_p ("dllimport", name))
86 DECL_EXTERNAL (*node) = 1;
87 TREE_PUBLIC (*node) = 1;
90 return NULL_TREE;
93 /* Handle a "shared" attribute;
94 arguments as in struct attribute_spec.handler. */
95 tree
96 ix86_handle_shared_attribute (node, name, args, flags, no_add_attrs)
97 tree *node;
98 tree name;
99 tree args ATTRIBUTE_UNUSED;
100 int flags ATTRIBUTE_UNUSED;
101 bool *no_add_attrs;
103 if (TREE_CODE (*node) != VAR_DECL)
105 warning ("`%s' attribute only applies to variables",
106 IDENTIFIER_POINTER (name));
107 *no_add_attrs = true;
110 return NULL_TREE;
113 /* Return the type that we should use to determine if DECL is
114 imported or exported. */
116 static tree
117 associated_type (decl)
118 tree decl;
120 tree t = NULL_TREE;
122 /* In the C++ frontend, DECL_CONTEXT for a method doesn't actually refer
123 to the containing class. So we look at the 'this' arg. */
124 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
126 /* Artificial methods are not affected by the import/export status of
127 their class unless they are virtual. */
128 if (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl))
129 t = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl))));
131 else if (DECL_CONTEXT (decl)
132 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl))) == 't')
133 t = DECL_CONTEXT (decl);
135 return t;
138 /* Return nonzero if DECL is a dllexport'd object. */
141 i386_pe_dllexport_p (decl)
142 tree decl;
144 tree exp;
146 if (TREE_CODE (decl) != VAR_DECL
147 && TREE_CODE (decl) != FUNCTION_DECL)
148 return 0;
149 exp = lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl));
150 if (exp)
151 return 1;
153 /* Class members get the dllexport status of their class. */
154 if (associated_type (decl))
156 exp = lookup_attribute ("dllexport",
157 TYPE_ATTRIBUTES (associated_type (decl)));
158 if (exp)
159 return 1;
162 return 0;
165 /* Return nonzero if DECL is a dllimport'd object. */
168 i386_pe_dllimport_p (decl)
169 tree decl;
171 tree imp;
173 if (TREE_CODE (decl) == FUNCTION_DECL
174 && TARGET_NOP_FUN_DLLIMPORT)
175 return 0;
177 if (TREE_CODE (decl) != VAR_DECL
178 && TREE_CODE (decl) != FUNCTION_DECL)
179 return 0;
180 imp = lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl));
181 if (imp)
182 return 1;
184 /* Class members get the dllimport status of their class. */
185 if (associated_type (decl))
187 imp = lookup_attribute ("dllimport",
188 TYPE_ATTRIBUTES (associated_type (decl)));
189 if (imp)
190 return 1;
193 return 0;
196 /* Return nonzero if SYMBOL is marked as being dllexport'd. */
199 i386_pe_dllexport_name_p (symbol)
200 const char *symbol;
202 return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
205 /* Return nonzero if SYMBOL is marked as being dllimport'd. */
208 i386_pe_dllimport_name_p (symbol)
209 const char *symbol;
211 return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
214 /* Mark a DECL as being dllexport'd.
215 Note that we override the previous setting (eg: dllimport). */
217 void
218 i386_pe_mark_dllexport (decl)
219 tree decl;
221 const char *oldname;
222 char *newname;
223 rtx rtlname;
224 tree idp;
226 rtlname = XEXP (DECL_RTL (decl), 0);
227 if (GET_CODE (rtlname) == SYMBOL_REF)
228 oldname = XSTR (rtlname, 0);
229 else if (GET_CODE (rtlname) == MEM
230 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
231 oldname = XSTR (XEXP (rtlname, 0), 0);
232 else
233 abort ();
234 if (i386_pe_dllimport_name_p (oldname))
235 oldname += 9;
236 else if (i386_pe_dllexport_name_p (oldname))
237 return; /* already done */
239 newname = alloca (strlen (oldname) + 4);
240 sprintf (newname, "@e.%s", oldname);
242 /* We pass newname through get_identifier to ensure it has a unique
243 address. RTL processing can sometimes peek inside the symbol ref
244 and compare the string's addresses to see if two symbols are
245 identical. */
246 idp = get_identifier (newname);
248 XEXP (DECL_RTL (decl), 0) =
249 gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
252 /* Mark a DECL as being dllimport'd. */
254 void
255 i386_pe_mark_dllimport (decl)
256 tree decl;
258 const char *oldname;
259 char *newname;
260 tree idp;
261 rtx rtlname, newrtl;
263 rtlname = XEXP (DECL_RTL (decl), 0);
264 if (GET_CODE (rtlname) == SYMBOL_REF)
265 oldname = XSTR (rtlname, 0);
266 else if (GET_CODE (rtlname) == MEM
267 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
268 oldname = XSTR (XEXP (rtlname, 0), 0);
269 else
270 abort ();
271 if (i386_pe_dllexport_name_p (oldname))
273 error ("`%s' declared as both exported to and imported from a DLL",
274 IDENTIFIER_POINTER (DECL_NAME (decl)));
275 return;
277 else if (i386_pe_dllimport_name_p (oldname))
279 /* Already done, but force correct linkage since the redeclaration
280 might have omitted explicit extern. Sigh. */
281 if (TREE_CODE (decl) == VAR_DECL
282 /* ??? Is this test for vtables needed? */
283 && !DECL_VIRTUAL_P (decl))
285 DECL_EXTERNAL (decl) = 1;
286 TREE_PUBLIC (decl) = 1;
288 return;
291 /* ??? One can well ask why we're making these checks here,
292 and that would be a good question. */
294 /* Imported variables can't be initialized. Note that C++ classes
295 are marked initial, so we need to check. */
296 if (TREE_CODE (decl) == VAR_DECL
297 && !DECL_VIRTUAL_P (decl)
298 && (DECL_INITIAL (decl)
299 && ! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
301 error_with_decl (decl, "initialized variable `%s' is marked dllimport");
302 return;
304 /* Nor can they be static. */
305 if (TREE_CODE (decl) == VAR_DECL
306 /* ??? Is this test for vtables needed? */
307 && !DECL_VIRTUAL_P (decl)
308 && 0 /*???*/)
310 error_with_decl (decl, "static variable `%s' is marked dllimport");
311 return;
314 newname = alloca (strlen (oldname) + 11);
315 sprintf (newname, "@i._imp__%s", oldname);
317 /* We pass newname through get_identifier to ensure it has a unique
318 address. RTL processing can sometimes peek inside the symbol ref
319 and compare the string's addresses to see if two symbols are
320 identical. */
321 idp = get_identifier (newname);
323 newrtl = gen_rtx (MEM, Pmode,
324 gen_rtx (SYMBOL_REF, Pmode,
325 IDENTIFIER_POINTER (idp)));
326 XEXP (DECL_RTL (decl), 0) = newrtl;
328 /* Can't treat a pointer to this as a constant address */
329 DECL_NON_ADDR_CONST_P (decl) = 1;
332 /* Return string which is the former assembler name modified with a
333 suffix consisting of an atsign (@) followed by the number of bytes of
334 arguments */
336 const char *
337 gen_stdcall_suffix (decl)
338 tree decl;
340 int total = 0;
341 /* ??? This probably should use XSTR (XEXP (DECL_RTL (decl), 0), 0) instead
342 of DECL_ASSEMBLER_NAME. */
343 const char *asmname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
344 char *newsym;
346 if (TYPE_ARG_TYPES (TREE_TYPE (decl)))
347 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl))))
348 == void_type_node)
350 tree formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
352 while (TREE_VALUE (formal_type) != void_type_node)
354 int parm_size
355 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type)));
356 /* Must round up to include padding. This is done the same
357 way as in store_one_arg. */
358 parm_size = ((parm_size + PARM_BOUNDARY - 1)
359 / PARM_BOUNDARY * PARM_BOUNDARY);
360 total += parm_size;
361 formal_type = TREE_CHAIN (formal_type);
365 newsym = xmalloc (strlen (asmname) + 10);
366 sprintf (newsym, "%s@%d", asmname, total/BITS_PER_UNIT);
367 return IDENTIFIER_POINTER (get_identifier (newsym));
370 void
371 i386_pe_encode_section_info (decl, first)
372 tree decl;
373 int first;
375 if (!first)
376 return;
378 /* This bit is copied from i386.h. */
379 if (optimize > 0 && TREE_CONSTANT (decl)
380 && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
382 rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
383 ? TREE_CST_RTL (decl) : DECL_RTL (decl));
384 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
387 if (TREE_CODE (decl) == FUNCTION_DECL)
388 if (lookup_attribute ("stdcall",
389 TYPE_ATTRIBUTES (TREE_TYPE (decl))))
390 XEXP (DECL_RTL (decl), 0) =
391 gen_rtx (SYMBOL_REF, Pmode, gen_stdcall_suffix (decl));
393 /* Mark the decl so we can tell from the rtl whether the object is
394 dllexport'd or dllimport'd. */
396 if (i386_pe_dllexport_p (decl))
397 i386_pe_mark_dllexport (decl);
398 else if (i386_pe_dllimport_p (decl))
399 i386_pe_mark_dllimport (decl);
400 /* It might be that DECL has already been marked as dllimport, but a
401 subsequent definition nullified that. The attribute is gone but
402 DECL_RTL still has @i._imp__foo. We need to remove that. Ditto
403 for the DECL_NON_ADDR_CONST_P flag. */
404 else if ((TREE_CODE (decl) == FUNCTION_DECL
405 || TREE_CODE (decl) == VAR_DECL)
406 && DECL_RTL (decl) != NULL_RTX
407 && GET_CODE (DECL_RTL (decl)) == MEM
408 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
409 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
410 && i386_pe_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
412 const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
413 tree idp = get_identifier (oldname + 9);
414 rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
416 XEXP (DECL_RTL (decl), 0) = newrtl;
418 DECL_NON_ADDR_CONST_P (decl) = 0;
420 /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
421 We leave these alone for now. */
425 /* Strip only the leading encoding, leaving the stdcall suffix. */
427 const char *
428 i386_pe_strip_name_encoding (str)
429 const char *str;
431 if (*str == '@')
432 str += 3;
433 if (*str == '*')
434 str += 1;
435 return str;
438 /* Also strip the stdcall suffix. */
440 const char *
441 i386_pe_strip_name_encoding_full (str)
442 const char *str;
444 const char *p;
445 const char *name = i386_pe_strip_name_encoding (str);
447 p = strchr (name, '@');
448 if (p)
449 return ggc_alloc_string (name, p - name);
451 return name;
454 void
455 i386_pe_unique_section (decl, reloc)
456 tree decl;
457 int reloc;
459 int len;
460 const char *name, *prefix;
461 char *string;
463 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
464 name = i386_pe_strip_name_encoding_full (name);
466 /* The object is put in, for example, section .text$foo.
467 The linker will then ultimately place them in .text
468 (everything from the $ on is stripped). Don't put
469 read-only data in .rdata section to avoid a PE linker
470 bug when .rdata$* grouped sections are used in code
471 without a .rdata section. */
472 if (TREE_CODE (decl) == FUNCTION_DECL)
473 prefix = ".text$";
474 else if (decl_readonly_section (decl, reloc))
475 prefix = ".rdata$";
476 else
477 prefix = ".data$";
478 len = strlen (name) + strlen (prefix);
479 string = alloca (len + 1);
480 sprintf (string, "%s%s", prefix, name);
482 DECL_SECTION_NAME (decl) = build_string (len, string);
485 /* Select a set of attributes for section NAME based on the properties
486 of DECL and whether or not RELOC indicates that DECL's initializer
487 might contain runtime relocations.
489 We make the section read-only and executable for a function decl,
490 read-only for a const data decl, and writable for a non-const data decl.
492 If the section has already been defined, to not allow it to have
493 different attributes, as (1) this is ambiguous since we're not seeing
494 all the declarations up front and (2) some assemblers (e.g. SVR4)
495 do not recoginize section redefinitions. */
496 /* ??? This differs from the "standard" PE implementation in that we
497 handle the SHARED variable attribute. Should this be done for all
498 PE targets? */
500 #define SECTION_PE_SHARED SECTION_MACH_DEP
502 unsigned int
503 i386_pe_section_type_flags (decl, name, reloc)
504 tree decl;
505 const char *name;
506 int reloc;
508 static htab_t htab;
509 unsigned int flags;
510 unsigned int **slot;
512 /* The names we put in the hashtable will always be the unique
513 versions gived to us by the stringtable, so we can just use
514 their addresses as the keys. */
515 if (!htab)
516 htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
518 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
519 flags = SECTION_CODE;
520 else if (decl && decl_readonly_section (decl, reloc))
521 flags = 0;
522 else
524 flags = SECTION_WRITE;
526 if (decl && TREE_CODE (decl) == VAR_DECL
527 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
528 flags |= SECTION_PE_SHARED;
531 if (decl && DECL_ONE_ONLY (decl))
532 flags |= SECTION_LINKONCE;
534 /* See if we already have an entry for this section. */
535 slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
536 if (!*slot)
538 *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
539 **slot = flags;
541 else
543 if (decl && **slot != flags)
544 error_with_decl (decl, "%s causes a section type conflict");
547 return flags;
550 void
551 i386_pe_asm_named_section (name, flags)
552 const char *name;
553 unsigned int flags;
555 char flagchars[8], *f = flagchars;
557 if (flags & SECTION_CODE)
558 *f++ = 'x';
559 if (flags & SECTION_WRITE)
560 *f++ = 'w';
561 if (flags & SECTION_PE_SHARED)
562 *f++ = 's';
563 *f = '\0';
565 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
567 if (flags & SECTION_LINKONCE)
569 /* Functions may have been compiled at various levels of
570 optimization so we can't use `same_size' here.
571 Instead, have the linker pick one. */
572 fprintf (asm_out_file, "\t.linkonce %s\n",
573 (flags & SECTION_CODE ? "discard" : "same_size"));
577 /* The Microsoft linker requires that every function be marked as
578 DT_FCN. When using gas on cygwin, we must emit appropriate .type
579 directives. */
581 #include "gsyms.h"
583 /* Mark a function appropriately. This should only be called for
584 functions for which we are not emitting COFF debugging information.
585 FILE is the assembler output file, NAME is the name of the
586 function, and PUBLIC is nonzero if the function is globally
587 visible. */
589 void
590 i386_pe_declare_function_type (file, name, public)
591 FILE *file;
592 const char *name;
593 int public;
595 fprintf (file, "\t.def\t");
596 assemble_name (file, name);
597 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
598 public ? (int) C_EXT : (int) C_STAT,
599 (int) DT_FCN << N_BTSHFT);
602 /* Keep a list of external functions. */
604 struct extern_list
606 struct extern_list *next;
607 const char *name;
610 static struct extern_list *extern_head;
612 /* Assemble an external function reference. We need to keep a list of
613 these, so that we can output the function types at the end of the
614 assembly. We can't output the types now, because we might see a
615 definition of the function later on and emit debugging information
616 for it then. */
618 void
619 i386_pe_record_external_function (name)
620 const char *name;
622 struct extern_list *p;
624 p = (struct extern_list *) xmalloc (sizeof *p);
625 p->next = extern_head;
626 p->name = name;
627 extern_head = p;
630 /* Keep a list of exported symbols. */
632 struct export_list
634 struct export_list *next;
635 const char *name;
636 int is_data; /* used to type tag exported symbols. */
639 static struct export_list *export_head;
641 /* Assemble an export symbol entry. We need to keep a list of
642 these, so that we can output the export list at the end of the
643 assembly. We used to output these export symbols in each function,
644 but that causes problems with GNU ld when the sections are
645 linkonce. */
647 void
648 i386_pe_record_exported_symbol (name, is_data)
649 const char *name;
650 int is_data;
652 struct export_list *p;
654 p = (struct export_list *) xmalloc (sizeof *p);
655 p->next = export_head;
656 p->name = name;
657 p->is_data = is_data;
658 export_head = p;
661 /* This is called at the end of assembly. For each external function
662 which has not been defined, we output a declaration now. We also
663 output the .drectve section. */
665 void
666 i386_pe_asm_file_end (file)
667 FILE *file;
669 struct extern_list *p;
671 ix86_asm_file_end (file);
673 for (p = extern_head; p != NULL; p = p->next)
675 tree decl;
677 decl = get_identifier (p->name);
679 /* Positively ensure only one declaration for any given symbol. */
680 if (! TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (decl))
682 TREE_ASM_WRITTEN (decl) = 1;
683 i386_pe_declare_function_type (file, p->name, TREE_PUBLIC (decl));
687 if (export_head)
689 struct export_list *q;
690 drectve_section ();
691 for (q = export_head; q != NULL; q = q->next)
693 fprintf (file, "\t.ascii \" -export:%s%s\"\n",
694 i386_pe_strip_name_encoding (q->name),
695 (q->is_data) ? ",data" : "");