#include "config.h"
#include "system.h"
#include "rtl.h"
#include "regs.h"
#include "hard-reg-set.h"
#include "output.h"
#include "tree.h"
#include "flags.h"
#include "tm_p.h"
#include "toplev.h"
#include "hashtab.h"
#include "ggc.h"
static tree associated_type PARAMS ((tree));
const char * gen_stdcall_suffix PARAMS ((tree));
int i386_pe_dllexport_p PARAMS ((tree));
int i386_pe_dllimport_p PARAMS ((tree));
void i386_pe_mark_dllexport PARAMS ((tree));
void i386_pe_mark_dllimport PARAMS ((tree));
tree
ix86_handle_dll_attribute (node, name, args, flags, no_add_attrs)
tree *node;
tree name;
tree args;
int flags;
bool *no_add_attrs;
{
if (!DECL_P (*node))
{
if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
| (int) ATTR_FLAG_ARRAY_NEXT))
{
*no_add_attrs = true;
return tree_cons (name, args, NULL_TREE);
}
if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
{
warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
*no_add_attrs = true;
}
}
else if (TREE_CODE (*node) == VAR_DECL
&& is_attribute_p ("dllimport", name))
{
DECL_EXTERNAL (*node) = 1;
TREE_PUBLIC (*node) = 1;
}
return NULL_TREE;
}
tree
ix86_handle_shared_attribute (node, name, args, flags, no_add_attrs)
tree *node;
tree name;
tree args ATTRIBUTE_UNUSED;
int flags ATTRIBUTE_UNUSED;
bool *no_add_attrs;
{
if (TREE_CODE (*node) != VAR_DECL)
{
warning ("`%s' attribute only applies to variables",
IDENTIFIER_POINTER (name));
*no_add_attrs = true;
}
return NULL_TREE;
}
static tree
associated_type (decl)
tree decl;
{
tree t = NULL_TREE;
if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
{
if (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl))
t = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl))));
}
else if (DECL_CONTEXT (decl)
&& TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl))) == 't')
t = DECL_CONTEXT (decl);
return t;
}
int
i386_pe_dllexport_p (decl)
tree decl;
{
tree exp;
if (TREE_CODE (decl) != VAR_DECL
&& TREE_CODE (decl) != FUNCTION_DECL)
return 0;
exp = lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl));
if (exp)
return 1;
if (associated_type (decl))
{
exp = lookup_attribute ("dllexport",
TYPE_ATTRIBUTES (associated_type (decl)));
if (exp)
return 1;
}
return 0;
}
int
i386_pe_dllimport_p (decl)
tree decl;
{
tree imp;
if (TREE_CODE (decl) == FUNCTION_DECL
&& TARGET_NOP_FUN_DLLIMPORT)
return 0;
if (TREE_CODE (decl) != VAR_DECL
&& TREE_CODE (decl) != FUNCTION_DECL)
return 0;
imp = lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl));
if (imp)
return 1;
if (associated_type (decl))
{
imp = lookup_attribute ("dllimport",
TYPE_ATTRIBUTES (associated_type (decl)));
if (imp)
return 1;
}
return 0;
}
int
i386_pe_dllexport_name_p (symbol)
const char *symbol;
{
return symbol[0] == DLL_IMPORT_EXPORT_PREFIX
&& symbol[1] == 'e' && symbol[2] == '.';
}
int
i386_pe_dllimport_name_p (symbol)
const char *symbol;
{
return symbol[0] == DLL_IMPORT_EXPORT_PREFIX
&& symbol[1] == 'i' && symbol[2] == '.';
}
void
i386_pe_mark_dllexport (decl)
tree decl;
{
const char *oldname;
char *newname;
rtx rtlname;
tree idp;
rtlname = XEXP (DECL_RTL (decl), 0);
if (GET_CODE (rtlname) == SYMBOL_REF)
oldname = XSTR (rtlname, 0);
else if (GET_CODE (rtlname) == MEM
&& GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
oldname = XSTR (XEXP (rtlname, 0), 0);
else
abort ();
if (i386_pe_dllimport_name_p (oldname))
oldname += 9;
else if (i386_pe_dllexport_name_p (oldname))
return;
newname = alloca (strlen (oldname) + 4);
sprintf (newname, "%ce.%s", DLL_IMPORT_EXPORT_PREFIX, oldname);
idp = get_identifier (newname);
XEXP (DECL_RTL (decl), 0) =
gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
}
void
i386_pe_mark_dllimport (decl)
tree decl;
{
const char *oldname;
char *newname;
tree idp;
rtx rtlname, newrtl;
rtlname = XEXP (DECL_RTL (decl), 0);
if (GET_CODE (rtlname) == SYMBOL_REF)
oldname = XSTR (rtlname, 0);
else if (GET_CODE (rtlname) == MEM
&& GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
oldname = XSTR (XEXP (rtlname, 0), 0);
else
abort ();
if (i386_pe_dllexport_name_p (oldname))
{
error ("`%s' declared as both exported to and imported from a DLL",
IDENTIFIER_POINTER (DECL_NAME (decl)));
return;
}
else if (i386_pe_dllimport_name_p (oldname))
{
if (TREE_CODE (decl) == VAR_DECL
&& !DECL_VIRTUAL_P (decl))
{
DECL_EXTERNAL (decl) = 1;
TREE_PUBLIC (decl) = 1;
}
return;
}
if (TREE_CODE (decl) == VAR_DECL
&& !DECL_VIRTUAL_P (decl)
&& (DECL_INITIAL (decl)
&& ! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
{
error_with_decl (decl, "initialized variable `%s' is marked dllimport");
return;
}
if (TREE_CODE (decl) == VAR_DECL
&& !DECL_VIRTUAL_P (decl)
&& 0 )
{
error_with_decl (decl, "static variable `%s' is marked dllimport");
return;
}
newname = alloca (strlen (oldname) + 11);
sprintf (newname, "%ci._imp__%s", DLL_IMPORT_EXPORT_PREFIX, oldname);
idp = get_identifier (newname);
newrtl = gen_rtx (MEM, Pmode,
gen_rtx (SYMBOL_REF, Pmode,
IDENTIFIER_POINTER (idp)));
XEXP (DECL_RTL (decl), 0) = newrtl;
DECL_NON_ADDR_CONST_P (decl) = 1;
}
const char *
gen_stdcall_suffix (decl)
tree decl;
{
int total = 0;
const char *asmname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
char *newsym;
if (TYPE_ARG_TYPES (TREE_TYPE (decl)))
if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl))))
== void_type_node)
{
tree formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
while (TREE_VALUE (formal_type) != void_type_node)
{
int parm_size
= TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type)));
parm_size = ((parm_size + PARM_BOUNDARY - 1)
/ PARM_BOUNDARY * PARM_BOUNDARY);
total += parm_size;
formal_type = TREE_CHAIN (formal_type);
}
}
newsym = xmalloc (strlen (asmname) + 10);
sprintf (newsym, "%s@%d", asmname, total/BITS_PER_UNIT);
return IDENTIFIER_POINTER (get_identifier (newsym));
}
void
i386_pe_encode_section_info (decl, first)
tree decl;
int first;
{
if (!first)
return;
if (optimize > 0 && TREE_CONSTANT (decl)
&& (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
{
rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
? TREE_CST_RTL (decl) : DECL_RTL (decl));
SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
}
if (TREE_CODE (decl) == FUNCTION_DECL)
if (lookup_attribute ("stdcall",
TYPE_ATTRIBUTES (TREE_TYPE (decl))))
XEXP (DECL_RTL (decl), 0) =
gen_rtx (SYMBOL_REF, Pmode, gen_stdcall_suffix (decl));
if (i386_pe_dllexport_p (decl))
i386_pe_mark_dllexport (decl);
else if (i386_pe_dllimport_p (decl))
i386_pe_mark_dllimport (decl);
else if ((TREE_CODE (decl) == FUNCTION_DECL
|| TREE_CODE (decl) == VAR_DECL)
&& DECL_RTL (decl) != NULL_RTX
&& GET_CODE (DECL_RTL (decl)) == MEM
&& GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
&& GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
&& i386_pe_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
{
const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
tree idp = get_identifier (oldname + 9);
rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
XEXP (DECL_RTL (decl), 0) = newrtl;
DECL_NON_ADDR_CONST_P (decl) = 0;
}
}
const char *
i386_pe_strip_name_encoding (str)
const char *str;
{
if (*str == DLL_IMPORT_EXPORT_PREFIX)
str += 3;
if (*str == '*')
str += 1;
return str;
}
const char *
i386_pe_strip_name_encoding_full (str)
const char *str;
{
const char *p;
const char *name = i386_pe_strip_name_encoding (str);
p = strchr (name, '@');
if (p)
return ggc_alloc_string (name, p - name);
return name;
}
void
i386_pe_unique_section (decl, reloc)
tree decl;
int reloc;
{
int len;
const char *name, *prefix;
char *string;
name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
name = i386_pe_strip_name_encoding_full (name);
if (TREE_CODE (decl) == FUNCTION_DECL)
prefix = ".text$";
else if (decl_readonly_section (decl, reloc))
prefix = ".rdata$";
else
prefix = ".data$";
len = strlen (name) + strlen (prefix);
string = alloca (len + 1);
sprintf (string, "%s%s", prefix, name);
DECL_SECTION_NAME (decl) = build_string (len, string);
}
#define SECTION_PE_SHARED SECTION_MACH_DEP
unsigned int
i386_pe_section_type_flags (decl, name, reloc)
tree decl;
const char *name;
int reloc;
{
static htab_t htab;
unsigned int flags;
unsigned int **slot;
if (!htab)
htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
if (decl && TREE_CODE (decl) == FUNCTION_DECL)
flags = SECTION_CODE;
else if (decl && decl_readonly_section (decl, reloc))
flags = 0;
else
{
flags = SECTION_WRITE;
if (decl && TREE_CODE (decl) == VAR_DECL
&& lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
flags |= SECTION_PE_SHARED;
}
if (decl && DECL_ONE_ONLY (decl))
flags |= SECTION_LINKONCE;
slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
if (!*slot)
{
*slot = (unsigned int *) xmalloc (sizeof (unsigned int));
**slot = flags;
}
else
{
if (decl && **slot != flags)
error_with_decl (decl, "%s causes a section type conflict");
}
return flags;
}
void
i386_pe_asm_named_section (name, flags)
const char *name;
unsigned int flags;
{
char flagchars[8], *f = flagchars;
if (flags & SECTION_CODE)
*f++ = 'x';
if (flags & SECTION_WRITE)
*f++ = 'w';
if (flags & SECTION_PE_SHARED)
*f++ = 's';
*f = '\0';
fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
if (flags & SECTION_LINKONCE)
{
fprintf (asm_out_file, "\t.linkonce %s\n",
(flags & SECTION_CODE ? "discard" : "same_size"));
}
}
#include "gsyms.h"
void
i386_pe_declare_function_type (file, name, public)
FILE *file;
const char *name;
int public;
{
fprintf (file, "\t.def\t");
assemble_name (file, name);
fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
public ? (int) C_EXT : (int) C_STAT,
(int) DT_FCN << N_BTSHFT);
}
struct extern_list
{
struct extern_list *next;
const char *name;
};
static struct extern_list *extern_head;
void
i386_pe_record_external_function (name)
const char *name;
{
struct extern_list *p;
p = (struct extern_list *) xmalloc (sizeof *p);
p->next = extern_head;
p->name = name;
extern_head = p;
}
struct export_list
{
struct export_list *next;
const char *name;
int is_data;
};
static struct export_list *export_head;
void
i386_pe_record_exported_symbol (name, is_data)
const char *name;
int is_data;
{
struct export_list *p;
p = (struct export_list *) xmalloc (sizeof *p);
p->next = export_head;
p->name = name;
p->is_data = is_data;
export_head = p;
}
void
i386_pe_asm_file_end (file)
FILE *file;
{
struct extern_list *p;
ix86_asm_file_end (file);
for (p = extern_head; p != NULL; p = p->next)
{
tree decl;
decl = get_identifier (p->name);
if (! TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (decl))
{
TREE_ASM_WRITTEN (decl) = 1;
i386_pe_declare_function_type (file, p->name, TREE_PUBLIC (decl));
}
}
if (export_head)
{
struct export_list *q;
drectve_section ();
for (q = export_head; q != NULL; q = q->next)
{
fprintf (file, "\t.ascii \" -export:%s%s\"\n",
i386_pe_strip_name_encoding (q->name),
(q->is_data) ? ",data" : "");
}
}
}