* config/i386/netbsd-elf.h (TARGET_OS_CPP_BUILTINS): Define.
[official-gcc.git] / gcc / config / i386 / winnt.c
blobd6f0a7a81aa18bb86edec2caf6fe64ce32816b3d
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 return NULL_TREE;
84 /* Handle a "shared" attribute;
85 arguments as in struct attribute_spec.handler. */
86 tree
87 ix86_handle_shared_attribute (node, name, args, flags, no_add_attrs)
88 tree *node;
89 tree name;
90 tree args ATTRIBUTE_UNUSED;
91 int flags ATTRIBUTE_UNUSED;
92 bool *no_add_attrs;
94 if (TREE_CODE (*node) != VAR_DECL)
96 warning ("`%s' attribute only applies to variables",
97 IDENTIFIER_POINTER (name));
98 *no_add_attrs = true;
101 return NULL_TREE;
104 /* Return the type that we should use to determine if DECL is
105 imported or exported. */
107 static tree
108 associated_type (decl)
109 tree decl;
111 tree t = NULL_TREE;
113 /* In the C++ frontend, DECL_CONTEXT for a method doesn't actually refer
114 to the containing class. So we look at the 'this' arg. */
115 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
117 /* Artificial methods are not affected by the import/export status of
118 their class unless they are virtual. */
119 if (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl))
120 t = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl))));
122 else if (DECL_CONTEXT (decl)
123 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl))) == 't')
124 t = DECL_CONTEXT (decl);
126 return t;
129 /* Return non-zero if DECL is a dllexport'd object. */
132 i386_pe_dllexport_p (decl)
133 tree decl;
135 tree exp;
137 if (TREE_CODE (decl) != VAR_DECL
138 && TREE_CODE (decl) != FUNCTION_DECL)
139 return 0;
140 exp = lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl));
141 if (exp)
142 return 1;
144 /* Class members get the dllexport status of their class. */
145 if (associated_type (decl))
147 exp = lookup_attribute ("dllexport",
148 TYPE_ATTRIBUTES (associated_type (decl)));
149 if (exp)
150 return 1;
153 return 0;
156 /* Return non-zero if DECL is a dllimport'd object. */
159 i386_pe_dllimport_p (decl)
160 tree decl;
162 tree imp;
164 if (TREE_CODE (decl) == FUNCTION_DECL
165 && TARGET_NOP_FUN_DLLIMPORT)
166 return 0;
168 if (TREE_CODE (decl) != VAR_DECL
169 && TREE_CODE (decl) != FUNCTION_DECL)
170 return 0;
171 imp = lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl));
172 if (imp)
173 return 1;
175 /* Class members get the dllimport status of their class. */
176 if (associated_type (decl))
178 imp = lookup_attribute ("dllimport",
179 TYPE_ATTRIBUTES (associated_type (decl)));
180 if (imp)
181 return 1;
184 return 0;
187 /* Return non-zero if SYMBOL is marked as being dllexport'd. */
190 i386_pe_dllexport_name_p (symbol)
191 const char *symbol;
193 return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
196 /* Return non-zero if SYMBOL is marked as being dllimport'd. */
199 i386_pe_dllimport_name_p (symbol)
200 const char *symbol;
202 return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
205 /* Mark a DECL as being dllexport'd.
206 Note that we override the previous setting (eg: dllimport). */
208 void
209 i386_pe_mark_dllexport (decl)
210 tree decl;
212 const char *oldname;
213 char *newname;
214 rtx rtlname;
215 tree idp;
217 rtlname = XEXP (DECL_RTL (decl), 0);
218 if (GET_CODE (rtlname) == SYMBOL_REF)
219 oldname = XSTR (rtlname, 0);
220 else if (GET_CODE (rtlname) == MEM
221 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
222 oldname = XSTR (XEXP (rtlname, 0), 0);
223 else
224 abort ();
225 if (i386_pe_dllimport_name_p (oldname))
226 oldname += 9;
227 else if (i386_pe_dllexport_name_p (oldname))
228 return; /* already done */
230 newname = alloca (strlen (oldname) + 4);
231 sprintf (newname, "@e.%s", oldname);
233 /* We pass newname through get_identifier to ensure it has a unique
234 address. RTL processing can sometimes peek inside the symbol ref
235 and compare the string's addresses to see if two symbols are
236 identical. */
237 idp = get_identifier (newname);
239 XEXP (DECL_RTL (decl), 0) =
240 gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
243 /* Mark a DECL as being dllimport'd. */
245 void
246 i386_pe_mark_dllimport (decl)
247 tree decl;
249 const char *oldname;
250 char *newname;
251 tree idp;
252 rtx rtlname, newrtl;
254 rtlname = XEXP (DECL_RTL (decl), 0);
255 if (GET_CODE (rtlname) == SYMBOL_REF)
256 oldname = XSTR (rtlname, 0);
257 else if (GET_CODE (rtlname) == MEM
258 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
259 oldname = XSTR (XEXP (rtlname, 0), 0);
260 else
261 abort ();
262 if (i386_pe_dllexport_name_p (oldname))
264 error ("`%s' declared as both exported to and imported from a DLL",
265 IDENTIFIER_POINTER (DECL_NAME (decl)));
266 return;
268 else if (i386_pe_dllimport_name_p (oldname))
270 /* Already done, but force correct linkage since the redeclaration
271 might have omitted explicit extern. Sigh. */
272 if (TREE_CODE (decl) == VAR_DECL
273 /* ??? Is this test for vtables needed? */
274 && !DECL_VIRTUAL_P (decl))
276 DECL_EXTERNAL (decl) = 1;
277 TREE_PUBLIC (decl) = 1;
279 return;
282 /* ??? One can well ask why we're making these checks here,
283 and that would be a good question. */
285 /* Imported variables can't be initialized. Note that C++ classes
286 are marked initial, so we need to check. */
287 if (TREE_CODE (decl) == VAR_DECL
288 && !DECL_VIRTUAL_P (decl)
289 && (DECL_INITIAL (decl)
290 && ! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
292 error_with_decl (decl, "initialized variable `%s' is marked dllimport");
293 return;
295 /* Nor can they be static. */
296 if (TREE_CODE (decl) == VAR_DECL
297 /* ??? Is this test for vtables needed? */
298 && !DECL_VIRTUAL_P (decl)
299 && 0 /*???*/)
301 error_with_decl (decl, "static variable `%s' is marked dllimport");
302 return;
305 /* `extern' needn't be specified with dllimport.
306 Specify `extern' now and hope for the best. Sigh. */
307 if (TREE_CODE (decl) == VAR_DECL
308 /* ??? Is this test for vtables needed? */
309 && !DECL_VIRTUAL_P (decl))
311 DECL_EXTERNAL (decl) = 1;
312 TREE_PUBLIC (decl) = 1;
315 newname = alloca (strlen (oldname) + 11);
316 sprintf (newname, "@i._imp__%s", oldname);
318 /* We pass newname through get_identifier to ensure it has a unique
319 address. RTL processing can sometimes peek inside the symbol ref
320 and compare the string's addresses to see if two symbols are
321 identical. */
322 idp = get_identifier (newname);
324 newrtl = gen_rtx (MEM, Pmode,
325 gen_rtx (SYMBOL_REF, Pmode,
326 IDENTIFIER_POINTER (idp)));
327 XEXP (DECL_RTL (decl), 0) = newrtl;
329 /* Can't treat a pointer to this as a constant address */
330 DECL_NON_ADDR_CONST_P (decl) = 1;
333 /* Return string which is the former assembler name modified with a
334 suffix consisting of an atsign (@) followed by the number of bytes of
335 arguments */
337 const char *
338 gen_stdcall_suffix (decl)
339 tree decl;
341 int total = 0;
342 /* ??? This probably should use XSTR (XEXP (DECL_RTL (decl), 0), 0) instead
343 of DECL_ASSEMBLER_NAME. */
344 const char *asmname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
345 char *newsym;
347 if (TYPE_ARG_TYPES (TREE_TYPE (decl)))
348 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl))))
349 == void_type_node)
351 tree formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
353 while (TREE_VALUE (formal_type) != void_type_node)
355 int parm_size
356 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type)));
357 /* Must round up to include padding. This is done the same
358 way as in store_one_arg. */
359 parm_size = ((parm_size + PARM_BOUNDARY - 1)
360 / PARM_BOUNDARY * PARM_BOUNDARY);
361 total += parm_size;
362 formal_type = TREE_CHAIN (formal_type);
366 newsym = xmalloc (strlen (asmname) + 10);
367 sprintf (newsym, "%s@%d", asmname, total/BITS_PER_UNIT);
368 return IDENTIFIER_POINTER (get_identifier (newsym));
371 void
372 i386_pe_encode_section_info (decl, first)
373 tree decl;
374 int first;
376 if (!first)
377 return;
379 /* This bit is copied from i386.h. */
380 if (optimize > 0 && TREE_CONSTANT (decl)
381 && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
383 rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
384 ? TREE_CST_RTL (decl) : DECL_RTL (decl));
385 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
388 if (TREE_CODE (decl) == FUNCTION_DECL)
389 if (lookup_attribute ("stdcall",
390 TYPE_ATTRIBUTES (TREE_TYPE (decl))))
391 XEXP (DECL_RTL (decl), 0) =
392 gen_rtx (SYMBOL_REF, Pmode, gen_stdcall_suffix (decl));
394 /* Mark the decl so we can tell from the rtl whether the object is
395 dllexport'd or dllimport'd. */
397 if (i386_pe_dllexport_p (decl))
398 i386_pe_mark_dllexport (decl);
399 else if (i386_pe_dllimport_p (decl))
400 i386_pe_mark_dllimport (decl);
401 /* It might be that DECL has already been marked as dllimport, but a
402 subsequent definition nullified that. The attribute is gone but
403 DECL_RTL still has @i._imp__foo. We need to remove that. Ditto
404 for the DECL_NON_ADDR_CONST_P flag. */
405 else if ((TREE_CODE (decl) == FUNCTION_DECL
406 || TREE_CODE (decl) == VAR_DECL)
407 && DECL_RTL (decl) != NULL_RTX
408 && GET_CODE (DECL_RTL (decl)) == MEM
409 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
410 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
411 && i386_pe_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
413 const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
414 tree idp = get_identifier (oldname + 9);
415 rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
417 XEXP (DECL_RTL (decl), 0) = newrtl;
419 DECL_NON_ADDR_CONST_P (decl) = 0;
421 /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
422 We leave these alone for now. */
426 /* Strip only the leading encoding, leaving the stdcall suffix. */
428 const char *
429 i386_pe_strip_name_encoding (str)
430 const char *str;
432 if (*str == '@')
433 str += 3;
434 if (*str == '*')
435 str += 1;
436 return str;
439 /* Also strip the stdcall suffix. */
441 const char *
442 i386_pe_strip_name_encoding_full (str)
443 const char *str;
445 const char *p;
446 const char *name = i386_pe_strip_name_encoding (str);
448 p = strchr (name, '@');
449 if (p)
450 return ggc_alloc_string (name, p - name);
452 return name;
455 void
456 i386_pe_unique_section (decl, reloc)
457 tree decl;
458 int reloc;
460 int len;
461 const char *name, *prefix;
462 char *string;
464 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
465 name = i386_pe_strip_name_encoding_full (name);
467 /* The object is put in, for example, section .text$foo.
468 The linker will then ultimately place them in .text
469 (everything from the $ on is stripped). Don't put
470 read-only data in .rdata section to avoid a PE linker
471 bug when .rdata$* grouped sections are used in code
472 without a .rdata section. */
473 if (TREE_CODE (decl) == FUNCTION_DECL)
474 prefix = ".text$";
475 else if (DECL_READONLY_SECTION (decl, reloc))
476 prefix = ".rdata$";
477 else
478 prefix = ".data$";
479 len = strlen (name) + strlen (prefix);
480 string = alloca (len + 1);
481 sprintf (string, "%s%s", prefix, name);
483 DECL_SECTION_NAME (decl) = build_string (len, string);
486 /* Select a set of attributes for section NAME based on the properties
487 of DECL and whether or not RELOC indicates that DECL's initializer
488 might contain runtime relocations.
490 We make the section read-only and executable for a function decl,
491 read-only for a const data decl, and writable for a non-const data decl.
493 If the section has already been defined, to not allow it to have
494 different attributes, as (1) this is ambiguous since we're not seeing
495 all the declarations up front and (2) some assemblers (e.g. SVR4)
496 do not recoginize section redefinitions. */
497 /* ??? This differs from the "standard" PE implementation in that we
498 handle the SHARED variable attribute. Should this be done for all
499 PE targets? */
501 #define SECTION_PE_SHARED SECTION_MACH_DEP
503 unsigned int
504 i386_pe_section_type_flags (decl, name, reloc)
505 tree decl;
506 const char *name;
507 int reloc;
509 static htab_t htab;
510 unsigned int flags;
511 unsigned int **slot;
513 /* The names we put in the hashtable will always be the unique
514 versions gived to us by the stringtable, so we can just use
515 their addresses as the keys. */
516 if (!htab)
517 htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
519 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
520 flags = SECTION_CODE;
521 else if (decl && DECL_READONLY_SECTION (decl, reloc))
522 flags = 0;
523 else
525 flags = SECTION_WRITE;
527 if (decl && TREE_CODE (decl) == VAR_DECL
528 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
529 flags |= SECTION_PE_SHARED;
532 if (decl && DECL_ONE_ONLY (decl))
533 flags |= SECTION_LINKONCE;
535 /* See if we already have an entry for this section. */
536 slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
537 if (!*slot)
539 *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
540 **slot = flags;
542 else
544 if (decl && **slot != flags)
545 error_with_decl (decl, "%s causes a section type conflict");
548 return flags;
551 void
552 i386_pe_asm_named_section (name, flags)
553 const char *name;
554 unsigned int flags;
556 char flagchars[8], *f = flagchars;
558 if (flags & SECTION_CODE)
559 *f++ = 'x';
560 if (flags & SECTION_WRITE)
561 *f++ = 'w';
562 if (flags & SECTION_PE_SHARED)
563 *f++ = 's';
564 *f = '\0';
566 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
568 if (flags & SECTION_LINKONCE)
570 /* Functions may have been compiled at various levels of
571 optimization so we can't use `same_size' here.
572 Instead, have the linker pick one. */
573 fprintf (asm_out_file, "\t.linkonce %s\n",
574 (flags & SECTION_CODE ? "discard" : "same_size"));
578 /* The Microsoft linker requires that every function be marked as
579 DT_FCN. When using gas on cygwin, we must emit appropriate .type
580 directives. */
582 #include "gsyms.h"
584 /* Mark a function appropriately. This should only be called for
585 functions for which we are not emitting COFF debugging information.
586 FILE is the assembler output file, NAME is the name of the
587 function, and PUBLIC is non-zero if the function is globally
588 visible. */
590 void
591 i386_pe_declare_function_type (file, name, public)
592 FILE *file;
593 const char *name;
594 int public;
596 fprintf (file, "\t.def\t");
597 assemble_name (file, name);
598 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
599 public ? (int) C_EXT : (int) C_STAT,
600 (int) DT_FCN << N_BTSHFT);
603 /* Keep a list of external functions. */
605 struct extern_list
607 struct extern_list *next;
608 const char *name;
611 static struct extern_list *extern_head;
613 /* Assemble an external function reference. We need to keep a list of
614 these, so that we can output the function types at the end of the
615 assembly. We can't output the types now, because we might see a
616 definition of the function later on and emit debugging information
617 for it then. */
619 void
620 i386_pe_record_external_function (name)
621 const char *name;
623 struct extern_list *p;
625 p = (struct extern_list *) permalloc (sizeof *p);
626 p->next = extern_head;
627 p->name = name;
628 extern_head = p;
631 /* Keep a list of exported symbols. */
633 struct export_list
635 struct export_list *next;
636 const char *name;
637 int is_data; /* used to type tag exported symbols. */
640 static struct export_list *export_head;
642 /* Assemble an export symbol entry. We need to keep a list of
643 these, so that we can output the export list at the end of the
644 assembly. We used to output these export symbols in each function,
645 but that causes problems with GNU ld when the sections are
646 linkonce. */
648 void
649 i386_pe_record_exported_symbol (name, is_data)
650 const char *name;
651 int is_data;
653 struct export_list *p;
655 p = (struct export_list *) permalloc (sizeof *p);
656 p->next = export_head;
657 p->name = name;
658 p->is_data = is_data;
659 export_head = p;
662 /* This is called at the end of assembly. For each external function
663 which has not been defined, we output a declaration now. We also
664 output the .drectve section. */
666 void
667 i386_pe_asm_file_end (file)
668 FILE *file;
670 struct extern_list *p;
672 ix86_asm_file_end (file);
674 for (p = extern_head; p != NULL; p = p->next)
676 tree decl;
678 decl = get_identifier (p->name);
680 /* Positively ensure only one declaration for any given symbol. */
681 if (! TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (decl))
683 TREE_ASM_WRITTEN (decl) = 1;
684 i386_pe_declare_function_type (file, p->name, TREE_PUBLIC (decl));
688 if (export_head)
690 struct export_list *q;
691 drectve_section ();
692 for (q = export_head; q != NULL; q = q->next)
694 fprintf (file, "\t.ascii \" -export:%s%s\"\n",
695 i386_pe_strip_name_encoding (q->name),
696 (q->is_data) ? ",data" : "");