]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/expr.c
expr.c (cplus_expand_expr): Only do PCC_STATIC_STRUCT_RETURN thing if (aggregate_valu...
[gcc.git] / gcc / cp / expr.c
1 /* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
3 Copyright (C) 1988, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include <stdio.h>
25 #include "rtl.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "expr.h"
29 #include "cp-tree.h"
30
31 static tree extract_aggr_init PROTO((tree, tree));
32 #if 0
33 static tree extract_scalar_init PROTO((tree, tree));
34 #endif
35 static rtx cplus_expand_expr PROTO((tree, rtx, enum machine_mode,
36 enum expand_modifier));
37
38 /* Hook used by expand_expr to expand language-specific tree codes. */
39
40 static rtx
41 cplus_expand_expr (exp, target, tmode, modifier)
42 tree exp;
43 rtx target;
44 enum machine_mode tmode;
45 enum expand_modifier modifier;
46 {
47 tree type = TREE_TYPE (exp);
48 register enum machine_mode mode = TYPE_MODE (type);
49 register enum tree_code code = TREE_CODE (exp);
50 int ignore = target == const0_rtx;
51
52 if (ignore)
53 target = 0;
54
55 /* No sense saving up arithmetic to be done
56 if it's all in the wrong mode to form part of an address.
57 And force_operand won't know whether to sign-extend or zero-extend. */
58
59 if (mode != Pmode && modifier == EXPAND_SUM)
60 modifier = EXPAND_NORMAL;
61
62 switch (code)
63 {
64 case AGGR_INIT_EXPR:
65 {
66 /* Something needs to be initialized, but we didn't know
67 where that thing was when building the tree. For example,
68 it could be the return value of a function, or a parameter
69 to a function which lays down in the stack, or a temporary
70 variable which must be passed by reference.
71
72 Cleanups are handled in a language-specific way: they
73 might be run by the called function (true in GNU C++
74 for parameters with cleanups), or they might be
75 run by the caller, after the call (true in GNU C++
76 for other cleanup needs). */
77
78 tree func = TREE_OPERAND (exp, 0);
79 tree args = TREE_OPERAND (exp, 1);
80 tree type = TREE_TYPE (exp), slot;
81 tree call_exp;
82 rtx call_target, return_target;
83 int pcc_struct_return = 0;
84
85 /* The expression `init' wants to initialize what
86 `target' represents. SLOT holds the slot for TARGET. */
87 slot = TREE_OPERAND (exp, 2);
88
89 /* Should always be called with a target. */
90 my_friendly_assert (target != NULL_RTX, 205);
91
92 /* The target the initializer will initialize (CALL_TARGET)
93 must now be directed to initialize the target we are
94 supposed to initialize (TARGET). The semantics for
95 choosing what CALL_TARGET is is language-specific,
96 as is building the call which will perform the
97 initialization. It is left here to show the choices that
98 exist for C++. */
99
100 if (TREE_CODE (func) == ADDR_EXPR
101 && TREE_CODE (TREE_OPERAND (func, 0)) == FUNCTION_DECL
102 && DECL_CONSTRUCTOR_P (TREE_OPERAND (func, 0)))
103 {
104 type = build_pointer_type (type);
105 /* Don't clobber a value that might be part of a default
106 parameter value. */
107 mark_addressable (slot);
108 if (TREE_PERMANENT (args))
109 args = expr_tree_cons (0, build1 (ADDR_EXPR, type, slot),
110 TREE_CHAIN (args));
111 else
112 TREE_VALUE (args) = build1 (ADDR_EXPR, type, slot);
113 call_target = 0;
114 }
115 else
116 {
117 call_target = target;
118 #ifdef PCC_STATIC_STRUCT_RETURN
119 if (aggregate_value_p (type))
120 {
121 pcc_struct_return = 1;
122 call_target = 0;
123 }
124 #endif
125 }
126
127 call_exp = build (CALL_EXPR, type, func, args, NULL_TREE);
128 TREE_SIDE_EFFECTS (call_exp) = 1;
129 return_target = expand_call (call_exp, call_target, ignore);
130
131 if (call_target)
132 /* Trust that the right thing has been done; it's too hard to
133 verify. */
134 return return_target;
135
136 /* If we're suffering under the ancient PCC_STATIC_STRUCT_RETURN
137 calling convention, we need to copy the return value out of
138 the static return buffer into slot. */
139 if (pcc_struct_return)
140 {
141 extern int flag_access_control;
142 int old_ac = flag_access_control;
143
144 tree init = build_decl (VAR_DECL, NULL_TREE,
145 build_reference_type (type));
146 DECL_RTL (init) = XEXP (return_target, 0);
147 init = convert_from_reference (init);
148
149 flag_access_control = 0;
150 expand_aggr_init (slot, init, 0, LOOKUP_ONLYCONVERTING);
151 flag_access_control = old_ac;
152
153 if (TYPE_NEEDS_DESTRUCTOR (type))
154 {
155 init = maybe_build_cleanup (init);
156 if (init != NULL_TREE)
157 expand_expr (init, const0_rtx, VOIDmode, 0);
158 }
159 }
160
161 return DECL_RTL (slot);
162 }
163
164 case OFFSET_REF:
165 {
166 #if 1
167 return expand_expr (default_conversion (resolve_offset_ref (exp)),
168 target, tmode, EXPAND_NORMAL);
169 #else
170 /* This is old crusty code, and does not handle all that the
171 resolve_offset_ref function does. (mrs) */
172 tree base = build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 0), 0);
173 tree offset = build_unary_op (ADDR_EXPR, TREE_OPERAND (exp, 1), 0);
174 return expand_expr (build (PLUS_EXPR, TREE_TYPE (exp), base, offset),
175 target, tmode, EXPAND_NORMAL);
176 #endif
177 }
178
179 case THUNK_DECL:
180 return DECL_RTL (exp);
181
182 case THROW_EXPR:
183 expand_throw (TREE_OPERAND (exp, 0));
184 return NULL;
185
186 case VEC_INIT_EXPR:
187 return expand_expr
188 (expand_vec_init
189 (NULL_TREE, TREE_OPERAND (exp, 0),
190 build_binary_op (MINUS_EXPR, TREE_OPERAND (exp, 2),
191 integer_one_node, 1),
192 TREE_OPERAND (exp, 1), 0), target, tmode, modifier);
193
194 case NEW_EXPR:
195 return expand_expr (build_new_1 (exp), target, tmode, modifier);
196
197 default:
198 break;
199 }
200 my_friendly_abort (40);
201 /* NOTREACHED */
202 return NULL;
203 }
204
205 void
206 init_cplus_expand ()
207 {
208 lang_expand_expr = cplus_expand_expr;
209 }
210
211 /* If DECL had its rtl moved from where callers expect it
212 to be, fix it up. RESULT is the nominal rtl for the RESULT_DECL,
213 which may be a pseudo instead of a hard register. */
214
215 void
216 fixup_result_decl (decl, result)
217 tree decl;
218 rtx result;
219 {
220 if (REG_P (result))
221 {
222 if (REGNO (result) >= FIRST_PSEUDO_REGISTER)
223 {
224 rtx real_decl_result;
225
226 #ifdef FUNCTION_OUTGOING_VALUE
227 real_decl_result
228 = FUNCTION_OUTGOING_VALUE (TREE_TYPE (decl), current_function_decl);
229 #else
230 real_decl_result
231 = FUNCTION_VALUE (TREE_TYPE (decl), current_function_decl);
232 #endif
233 REG_FUNCTION_VALUE_P (real_decl_result) = 1;
234 result = real_decl_result;
235 }
236 store_expr (decl, result, 0);
237 emit_insn (gen_rtx (USE, VOIDmode, result));
238 }
239 }
240
241 /* Expand this initialization inline and see if it's simple enough that
242 it can be done at compile-time. */
243
244 static tree
245 extract_aggr_init (decl, init)
246 tree decl, init;
247 {
248 return 0;
249 }
250
251 #if 0
252 static tree
253 extract_scalar_init (decl, init)
254 tree decl, init;
255 {
256 rtx value, insns, insn;
257 extern struct obstack temporary_obstack;
258 tree t = NULL_TREE;
259
260 push_obstacks (&temporary_obstack, &temporary_obstack);
261 start_sequence ();
262 value = expand_expr (init, NULL_RTX, VOIDmode, 0);
263 insns = get_insns ();
264 end_sequence ();
265 reg_scan (insns, max_reg_num (), 0);
266 jump_optimize (insns, 0, 0, 1);
267 pop_obstacks ();
268
269 for (insn = insns; insn; insn = NEXT_INSN (insn))
270 {
271 rtx r, to;
272
273 if (GET_CODE (insn) == NOTE)
274 continue;
275 else if (GET_CODE (insn) != INSN)
276 return 0;
277
278 r = PATTERN (insn);
279 if (GET_CODE (r) != SET)
280 return 0;
281
282 to = XEXP (r, 0);
283
284 if (! (to == value
285 || (GET_CODE (to) == SUBREG && XEXP (to, 0) == value)))
286 return 0;
287
288 r = XEXP (r, 1);
289
290 switch (GET_CODE (r))
291 {
292 case CONST_INT:
293 t = build_int_2 (XEXP (r, 0), 0);
294 break;
295 default:
296 return 0;
297 }
298 }
299
300 return t;
301 }
302 #endif
303
304 int
305 extract_init (decl, init)
306 tree decl, init;
307 {
308 return 0;
309
310 #if 0
311 if (IS_AGGR_TYPE (TREE_TYPE (decl))
312 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
313 init = extract_aggr_init (decl, init);
314 else
315 init = extract_scalar_init (decl, init);
316
317 if (init == NULL_TREE)
318 return 0;
319
320 DECL_INITIAL (decl) = init;
321 return 1;
322 #endif
323 }
324
325 void
326 do_case (start, end)
327 tree start, end;
328 {
329 tree value1 = NULL_TREE, value2 = NULL_TREE, label;
330
331 if (start != NULL_TREE && TREE_TYPE (start) != NULL_TREE
332 && POINTER_TYPE_P (TREE_TYPE (start)))
333 error ("pointers are not permitted as case values");
334
335 if (end && pedantic)
336 pedwarn ("ANSI C++ forbids range expressions in switch statement");
337
338 if (processing_template_decl)
339 {
340 add_tree (build_min_nt (CASE_LABEL, start, end));
341 return;
342 }
343
344 if (start)
345 value1 = check_cp_case_value (start);
346 if (end)
347 value2 = check_cp_case_value (end);
348
349 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
350
351 if (value1 != error_mark_node
352 && value2 != error_mark_node)
353 {
354 tree duplicate;
355 int success;
356
357 if (end)
358 success = pushcase_range (value1, value2, convert_and_check,
359 label, &duplicate);
360 else if (start)
361 success = pushcase (value1, convert_and_check, label, &duplicate);
362 else
363 success = pushcase (NULL_TREE, 0, label, &duplicate);
364
365 if (success == 1)
366 {
367 if (end)
368 error ("case label not within a switch statement");
369 else if (start)
370 cp_error ("case label `%E' not within a switch statement", start);
371 else
372 error ("default label not within a switch statement");
373 }
374 else if (success == 2)
375 {
376 if (end)
377 {
378 error ("duplicate (or overlapping) case value");
379 cp_error_at ("this is the first entry overlapping that value",
380 duplicate);
381 }
382 else if (start)
383 {
384 cp_error ("duplicate case value `%E'", start);
385 cp_error_at ("previously used here", duplicate);
386 }
387 else
388 {
389 error ("multiple default labels in one switch");
390 cp_error_at ("this is the first default label", duplicate);
391 }
392 }
393 else if (success == 3)
394 warning ("case value out of range");
395 else if (success == 4)
396 warning ("empty range specified");
397 else if (success == 5)
398 {
399 if (end)
400 error ("case label within scope of cleanup or variable array");
401 else if (! start)
402 error ("`default' label within scope of cleanup or variable array");
403 else
404 cp_error ("case label `%E' within scope of cleanup or variable array", start);
405 }
406 }
407 if (start)
408 define_case_label (label);
409 else
410 define_case_label (NULL_TREE);
411 }
This page took 0.057061 seconds and 5 git commands to generate.