summaryrefslogtreecommitdiff
path: root/gcc/config/i386/netware.c
blob: 2232dbf6e51170285dd9ca19194a736b0ae59cbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Subroutines for insn-output.c for NetWare.
   Contributed by Jan Beulich (jbeulich@novell.com)
   Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010
   Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.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 "diagnostic-core.h"
#include "langhooks.h"
#include "ggc.h"

/* Return string which is the function name, identified by ID, modified
   with PREFIX and a suffix consisting of an atsign (@) followed by the
   number of bytes of arguments.  If ID is NULL use the DECL_NAME as base.
   Return NULL if no change required.  */

static tree
gen_stdcall_or_fastcall_decoration (tree decl, tree id, char prefix)
{
  unsigned HOST_WIDE_INT total = 0;
  const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
  char *new_str;
  tree type = TREE_TYPE (decl);

  if (prototype_p (type))
    {
      tree arg;
      function_args_iterator args_iter;

      /* This attribute is ignored for variadic functions.  */ 
      if (stdarg_p (type))
	return NULL_TREE;

      /* Quit if we hit an incomplete type.  Error is reported
	 by convert_arguments in c-typeck.c or cp/typeck.c.  */
      FOREACH_FUNCTION_ARGS(type, arg, args_iter)
	{
	  HOST_WIDE_INT parm_size;
	  unsigned HOST_WIDE_INT parm_boundary_bytes;

	  if (! COMPLETE_TYPE_P (arg))
	    break;

	  parm_size = int_size_in_bytes (arg);
	  if (parm_size < 0)
	    break;

	  parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;

	  /* Must round up to include padding.  This is done the same
	     way as in store_one_arg.  */
	  total += (parm_size + parm_boundary_bytes - 1)
		   / parm_boundary_bytes * parm_boundary_bytes;
	}
    }

  new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 10 + 1);
  sprintf (new_str, "%c%s@" HOST_WIDE_INT_PRINT_UNSIGNED,
	   prefix, old_str, total);

  return get_identifier (new_str);
}

/* Return string which is the function name, identified by ID, modified
   with an _n@ prefix (where n represents the number of arguments passed in
   registers).  If ID is NULL use the DECL_NAME as base.
   Return NULL if no change required.  */

static tree
gen_regparm_prefix (tree decl, tree id, unsigned int nregs)
{
  unsigned HOST_WIDE_INT total = 0;
  const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
  char *new_str;
  tree type = TREE_TYPE (decl);

  if (prototype_p (type))
    {
      tree arg;
      function_args_iterator args_iter;

      /* This attribute is ignored for variadic functions.  */ 
      if (stdarg_p (type))
	return NULL_TREE;

      /* Quit if we hit an incomplete type.  Error is reported
	 by convert_arguments in c-typeck.c or cp/typeck.c.  */
      FOREACH_FUNCTION_ARGS(type, arg, args_iter)
	{
	  HOST_WIDE_INT parm_size;
	  unsigned HOST_WIDE_INT parm_boundary_bytes;

	  if (! COMPLETE_TYPE_P (arg))
	    break;

	  parm_size = int_size_in_bytes (arg);
	  if (parm_size < 0)
	    break;

	  parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;

	  /* Must round up to include padding.  This is done the same
	     way as in store_one_arg.  */
	  total += (parm_size + parm_boundary_bytes - 1)
		   / parm_boundary_bytes * parm_boundary_bytes;
	}
    }

  if (nregs > total / UNITS_PER_WORD)
    nregs = total / UNITS_PER_WORD;
  gcc_assert (nregs <= 9);
  new_str = XALLOCAVEC (char, 3 + strlen (old_str) + 1);
  sprintf (new_str, "_%u@%s", nregs, old_str);

  return get_identifier (new_str);
}

/* Maybe decorate and get a new identifier for the DECL of a stdcall or
   fastcall function. The original identifier is supplied in ID. */

static tree
i386_nlm_maybe_mangle_decl_assembler_name (tree decl, tree id)
{
  tree type_attributes = TYPE_ATTRIBUTES (TREE_TYPE (decl));
  tree new_id;

  if (lookup_attribute ("stdcall", type_attributes))
    new_id = gen_stdcall_or_fastcall_decoration (decl, id, '_');
  else if (lookup_attribute ("fastcall", type_attributes))
    new_id = gen_stdcall_or_fastcall_decoration (decl, id, FASTCALL_PREFIX);
  else if ((new_id = lookup_attribute ("regparm", type_attributes)))
    new_id = gen_regparm_prefix (decl, id,
		  TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (new_id))));
  else
    new_id = NULL_TREE;

  return new_id;
}

/* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
   in the language-independent default hook
   langhooks.c:lhd_set_decl_assembler_name ()
   and in cp/mangle.c:mangle_decl ().  */
tree
i386_nlm_mangle_decl_assembler_name (tree decl, tree id)
{
  tree new_id = TREE_CODE (decl) == FUNCTION_DECL
		? i386_nlm_maybe_mangle_decl_assembler_name (decl, id)
		: NULL_TREE;

  return (new_id ? new_id : id);
}

void
i386_nlm_encode_section_info (tree decl, rtx rtl, int first)
{
  default_encode_section_info (decl, rtl, first);

  if (TREE_CODE (decl) == FUNCTION_DECL
      /* Do not change the identifier if a verbatim asmspec
	 or if stdcall suffix already added.  */
      && *IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)) != '*'
      && !strchr (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), '@')
      /* FIXME:  Imported stdcall names are not modified by the Ada frontend.
	 Check and decorate the RTL name now.  */
      && strcmp (lang_hooks.name, "GNU Ada") == 0)
    {
      rtx symbol = XEXP (rtl, 0);
      tree new_id;
      tree old_id = DECL_ASSEMBLER_NAME (decl);

      gcc_assert (GET_CODE (symbol) == SYMBOL_REF);

      if ((new_id = i386_nlm_maybe_mangle_decl_assembler_name (decl, old_id)))
	XSTR (symbol, 0) = IDENTIFIER_POINTER (new_id);
    }
}

/* Strip the stdcall/fastcall/regparm pre-/suffix.  */

const char *
i386_nlm_strip_name_encoding (const char *str)
{
  const char *name = default_strip_name_encoding (str);

  if (*str != '*' && (*name == '_' || *name == '@'))
    {
      const char *p = strchr (name + 1, '@');

      if (p)
	{
	  ++name;
	  if (ISDIGIT (p[1]))
	    name = ggc_alloc_string (name, p - name);
	  else
	    {
	      gcc_assert (ISDIGIT (*name));
	      name++;
	      gcc_assert (name == p);
	    }
	}
    }
  return name;
}