aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/cil32/missing-protos.c
blob: d6736bd49c5ceb94d74b7c090fced811da4ecf3d (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
230
231
232
233
/* Function without prototype patching pass.

   Copyright (C) 2006-2009 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 2, 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 COPYING.  If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

Authors:
   Andrea Ornstein
   Erven Rohou
   Gabriele Svelto
   Thierry Lafage

Contact information at STMicroelectronics:
Andrea C. Ornstein      <andrea.ornstein@st.com>
Contact information at INRIA:
Erven Rohou             <erven.rohou@inria.fr>
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "flags.h"
#include "timevar.h"
#include "errors.h"
#include "tree.h"
#include "tree-pass.h"
#include "pointer-set.h"
#include "cil-refs.h"
#include "cil-stack.h"
#include "cil-stmt.h"
#include "cil-types.h"
#include "emit-cil.h"
#include "source-location.h"

/******************************************************************************
 * Globals                                                                    *
 ******************************************************************************/

/* Pointer-map used to map parameters to their equivalent local variable.  */
static struct pointer_map_t *args_to_locals;

/******************************************************************************
 * Local functions prototypes                                                 *
 ******************************************************************************/

static void create_replacement_args (void);
static void replace_ldargs (void);
static void replace_args (void);
static unsigned int missing_protos (void);
static bool missing_protos_gate (void);

/******************************************************************************
 * Pass implementation                                                        *
 ******************************************************************************/

/* Replaces the function arguments with local variables of the corresponding
   type. Code is then injected for copying the arguments in the replacement
   local variables.  */

static void
create_replacement_args (void)
{
  tree args;
  tree var;
  void **slot;

  args_to_locals = pointer_map_create ();

  for (args = DECL_ARGUMENTS (current_function_decl);
       args != NULL_TREE;
       args = TREE_CHAIN (args))
    {
      tree type = TREE_TYPE (args);
      tree promoted = DECL_ARG_TYPE (args);

      if ((type != promoted) || cil_pointer_type_p (type))
	{
	  var = create_tmp_var (type, IDENTIFIER_POINTER (DECL_NAME (args)));
	  slot = pointer_map_insert (args_to_locals, args);
	  gcc_assert (*slot == NULL);
	  *slot = var;
	}
    }
}

/* Replaces every instance of LDARG/LDARGA with a a LDLOC/LDLOCA to the
   replacement argument.  */

static void
replace_ldargs (void)
{
  cil_stmt_iterator csi;
  cil_stmt stmt;
  basic_block bb;
  enum cil_opcode opcode;
  void **slot;

  FOR_EACH_BB (bb)
    {
      for (csi = csi_start_bb (bb); !csi_end_p (csi); csi_next (&csi))
	{
	  stmt = csi_stmt (csi);
	  opcode = cil_opcode (stmt);

	  if (opcode == CIL_LDARG
	      || opcode == CIL_LDARGA
	      || opcode == CIL_STARG)
	    {
	      slot = pointer_map_contains (args_to_locals, cil_var (stmt));

	      if (slot)
		{
		  switch (opcode)
		    {
		    case CIL_LDARG:  opcode = CIL_LDLOC;  break;
		    case CIL_LDARGA: opcode = CIL_LDLOCA; break;
		    case CIL_STARG:  opcode = CIL_STLOC;  break;
		    default:
		      gcc_unreachable ();
		    }

		  stmt = cil_build_stmt_arg (opcode, (tree) *slot);
		  csi_replace (&csi, stmt); /* Also updates the locus.  */
		}
	    }
	}
    }
}

/* Initialize the replacement variables.  */

static void
replace_args (void)
{
  cil_seq seq = cil_seq_alloc ();
  cil_stmt_iterator csi = csi_start (seq);
  cil_stmt_iterator bb_csi = csi_start_bb (single_succ (ENTRY_BLOCK_PTR));
  cil_stmt stmt;
  tree args;
  void **slot;

  for (args = DECL_ARGUMENTS (current_function_decl);
       args != NULL_TREE;
       args = TREE_CHAIN (args))
    {
      slot = pointer_map_contains (args_to_locals, args);

      if (slot != NULL)
	{
          source_location sloc = source_location_from_tree (args);
	  stmt = cil_build_stmt_arg (CIL_LDARG, args);
	  cil_set_locus(stmt, sloc);
	  csi_insert_after (&csi, stmt, CSI_CONTINUE_LINKING);
	  stmt = cil_build_stmt_arg (CIL_STLOC, (tree) *slot);
	  cil_set_locus(stmt, sloc);
	  csi_insert_after (&csi, stmt, CSI_CONTINUE_LINKING);
	}
    }

  csi_insert_seq_before (&bb_csi, seq, CSI_SAME_STMT);
}


/* Main function of this pass. Looks for functions w/o a prototype and replaces
   their arguments with local variables of the correct types. All instances of
   LDARG are replaced with LDLOCs except for the ones used for initializing the
   replacement locals. Once the prototypeless function reaches the emission
   point it's prototype is then printed using the 'promoted' argument types.  */

static unsigned int
missing_protos (void)
{
  if (TYPE_ARG_TYPES (TREE_TYPE (current_function_decl)) == NULL_TREE)
    {
      create_replacement_args ();
      replace_ldargs ();
      replace_args ();
      pointer_map_destroy (args_to_locals);
    }

  return 0;
}

/* Gate function of the missing prototype functions patch pass.  */

static bool
missing_protos_gate (void)
{
  return 1;
}

/* Define the parameters of the missing-protos pass.  */

struct gimple_opt_pass pass_missing_protos =
{
 {
  GIMPLE_PASS,                          /* type */
  "missing_protos",                     /* name */
  missing_protos_gate,                  /* gate */
  missing_protos,                       /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_MISSING_PROTOS,                    /* tv_id */
  PROP_cfg,                             /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,                                    /* todo_flags_start */
  TODO_ggc_collect                      /* todo_flags_finish */
 }
};

/*
 * Local variables:
 * eval: (c-set-style "gnu")
 * End:
 */