]>
Commit | Line | Data |
---|---|---|
b9fdd12b | 1 | /* Output routines for CR16 processor. |
d353bf18 | 2 | Copyright (C) 2012-2015 Free Software Foundation, Inc. |
b9fdd12b | 3 | Contributed by KPIT Cummins Infosystems Limited. |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published | |
9 | by the Free Software Foundation; either version 3, or (at your | |
10 | option) any later version. | |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
13 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
15 | License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GCC; see the file COPYING3. If not see | |
19 | <http://www.gnu.org/licenses/>. */ | |
20 | ||
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
25 | #include "rtl.h" | |
b20a8bb4 | 26 | #include "alias.h" |
27 | #include "symtab.h" | |
b9fdd12b | 28 | #include "tree.h" |
b20a8bb4 | 29 | #include "fold-const.h" |
9ed99284 | 30 | #include "stor-layout.h" |
31 | #include "calls.h" | |
b9fdd12b | 32 | #include "tm_p.h" |
33 | #include "regs.h" | |
34 | #include "hard-reg-set.h" | |
35 | #include "insn-config.h" | |
36 | #include "conditions.h" | |
37 | #include "output.h" | |
38 | #include "insn-codes.h" | |
39 | #include "insn-attr.h" | |
40 | #include "flags.h" | |
41 | #include "except.h" | |
42 | #include "function.h" | |
43 | #include "recog.h" | |
d53441c8 | 44 | #include "expmed.h" |
45 | #include "dojump.h" | |
46 | #include "explow.h" | |
47 | #include "emit-rtl.h" | |
48 | #include "varasm.h" | |
49 | #include "stmt.h" | |
b9fdd12b | 50 | #include "expr.h" |
51 | #include "optabs.h" | |
52 | #include "diagnostic-core.h" | |
94ea8568 | 53 | #include "predict.h" |
54 | #include "dominance.h" | |
55 | #include "cfg.h" | |
56 | #include "cfgrtl.h" | |
57 | #include "cfganal.h" | |
58 | #include "lcm.h" | |
59 | #include "cfgbuild.h" | |
60 | #include "cfgcleanup.h" | |
b9fdd12b | 61 | #include "basic-block.h" |
62 | #include "target.h" | |
63 | #include "target-def.h" | |
64 | #include "df.h" | |
f7715905 | 65 | #include "builtins.h" |
b9fdd12b | 66 | |
67 | /* Definitions. */ | |
68 | ||
69 | /* Maximum number of register used for passing parameters. */ | |
70 | #define MAX_REG_FOR_PASSING_ARGS 6 | |
71 | ||
72 | /* Minimum number register used for passing parameters. */ | |
73 | #define MIN_REG_FOR_PASSING_ARGS 2 | |
74 | ||
75 | /* The maximum count of words supported in the assembly of the architecture in | |
76 | a push/pop instruction. */ | |
77 | #define MAX_COUNT 8 | |
78 | ||
79 | /* Predicate is true if the current function is a 'noreturn' function, | |
80 | i.e. it is qualified as volatile. */ | |
81 | #define FUNC_IS_NORETURN_P(decl) (TREE_THIS_VOLATILE (decl)) | |
82 | ||
83 | /* Predicate that holds when we need to save registers even for 'noreturn' | |
9d75589a | 84 | functions, to accommodate for unwinding. */ |
b9fdd12b | 85 | #define MUST_SAVE_REGS_P() \ |
86 | (flag_unwind_tables || (flag_exceptions && !UI_SJLJ)) | |
87 | ||
88 | /* Nonzero if the rtx X is a signed const int of n bits. */ | |
89 | #define RTX_SIGNED_INT_FITS_N_BITS(X, n) \ | |
90 | ((GET_CODE (X) == CONST_INT \ | |
91 | && SIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0) | |
92 | ||
93 | /* Nonzero if the rtx X is an unsigned const int of n bits. */ | |
94 | #define RTX_UNSIGNED_INT_FITS_N_BITS(X, n) \ | |
95 | ((GET_CODE (X) == CONST_INT \ | |
96 | && UNSIGNED_INT_FITS_N_BITS (INTVAL (X), n)) ? 1 : 0) | |
97 | ||
98 | /* Structure for stack computations. */ | |
99 | ||
100 | /* variable definitions in the struture | |
101 | args_size Number of bytes saved on the stack for local | |
102 | variables | |
103 | ||
104 | reg_size Number of bytes saved on the stack for | |
105 | non-scratch registers | |
106 | ||
107 | total_size The sum of 2 sizes: locals vars and padding byte | |
108 | for saving the registers. Used in expand_prologue() | |
109 | and expand_epilogue() | |
110 | ||
111 | last_reg_to_save Will hold the number of the last register the | |
112 | prologue saves, -1 if no register is saved | |
113 | ||
114 | save_regs[16] Each object in the array is a register number. | |
115 | Mark 1 for registers that need to be saved | |
116 | ||
117 | num_regs Number of registers saved | |
118 | ||
119 | initialized Non-zero if frame size already calculated, not | |
120 | used yet | |
121 | ||
122 | function_makes_calls Does the function make calls ? not used yet. */ | |
123 | ||
124 | struct cr16_frame_info | |
125 | { | |
126 | unsigned long var_size; | |
127 | unsigned long args_size; | |
128 | unsigned int reg_size; | |
129 | unsigned long total_size; | |
130 | long last_reg_to_save; | |
131 | long save_regs[FIRST_PSEUDO_REGISTER]; | |
132 | int num_regs; | |
133 | int initialized; | |
134 | int function_makes_calls; | |
135 | }; | |
136 | ||
137 | /* Current frame information calculated by cr16_compute_frame_size. */ | |
138 | static struct cr16_frame_info current_frame_info; | |
139 | ||
140 | /* Static Variables. */ | |
141 | ||
142 | /* Data model that was supplied by user via command line option | |
143 | This will be overridden in case of invalid combination | |
144 | of core and data model options are supplied. */ | |
145 | static enum data_model_type data_model = DM_DEFAULT; | |
146 | ||
147 | /* TARGETM Function Prototypes and forward declarations */ | |
148 | static void cr16_print_operand (FILE *, rtx, int); | |
149 | static void cr16_print_operand_address (FILE *, rtx); | |
150 | ||
151 | /* Stack layout and calling conventions. */ | |
152 | #undef TARGET_STRUCT_VALUE_RTX | |
153 | #define TARGET_STRUCT_VALUE_RTX cr16_struct_value_rtx | |
154 | #undef TARGET_RETURN_IN_MEMORY | |
155 | #define TARGET_RETURN_IN_MEMORY cr16_return_in_memory | |
156 | ||
157 | /* Target-specific uses of '__attribute__'. */ | |
158 | #undef TARGET_ATTRIBUTE_TABLE | |
159 | #define TARGET_ATTRIBUTE_TABLE cr16_attribute_table | |
160 | #undef TARGET_NARROW_VOLATILE_BITFIELD | |
161 | #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false | |
162 | ||
163 | /* EH related. */ | |
164 | #undef TARGET_UNWIND_WORD_MODE | |
165 | #define TARGET_UNWIND_WORD_MODE cr16_unwind_word_mode | |
166 | ||
167 | /* Override Options. */ | |
168 | #undef TARGET_OPTION_OVERRIDE | |
169 | #define TARGET_OPTION_OVERRIDE cr16_override_options | |
170 | ||
171 | /* Conditional register usuage. */ | |
172 | #undef TARGET_CONDITIONAL_REGISTER_USAGE | |
173 | #define TARGET_CONDITIONAL_REGISTER_USAGE cr16_conditional_register_usage | |
174 | ||
175 | /* Controlling register spills. */ | |
176 | #undef TARGET_CLASS_LIKELY_SPILLED_P | |
177 | #define TARGET_CLASS_LIKELY_SPILLED_P cr16_class_likely_spilled_p | |
178 | ||
179 | /* Passing function arguments. */ | |
180 | #undef TARGET_FUNCTION_ARG | |
181 | #define TARGET_FUNCTION_ARG cr16_function_arg | |
182 | #undef TARGET_FUNCTION_ARG_ADVANCE | |
183 | #define TARGET_FUNCTION_ARG_ADVANCE cr16_function_arg_advance | |
184 | #undef TARGET_RETURN_POPS_ARGS | |
185 | #define TARGET_RETURN_POPS_ARGS cr16_return_pops_args | |
186 | ||
187 | /* Initialize the GCC target structure. */ | |
188 | #undef TARGET_FRAME_POINTER_REQUIRED | |
189 | #define TARGET_FRAME_POINTER_REQUIRED cr16_frame_pointer_required | |
190 | #undef TARGET_CAN_ELIMINATE | |
191 | #define TARGET_CAN_ELIMINATE cr16_can_eliminate | |
192 | #undef TARGET_LEGITIMIZE_ADDRESS | |
193 | #define TARGET_LEGITIMIZE_ADDRESS cr16_legitimize_address | |
194 | #undef TARGET_LEGITIMATE_CONSTANT_P | |
195 | #define TARGET_LEGITIMATE_CONSTANT_P cr16_legitimate_constant_p | |
196 | #undef TARGET_LEGITIMATE_ADDRESS_P | |
197 | #define TARGET_LEGITIMATE_ADDRESS_P cr16_legitimate_address_p | |
198 | ||
199 | /* Returning function value. */ | |
200 | #undef TARGET_FUNCTION_VALUE | |
201 | #define TARGET_FUNCTION_VALUE cr16_function_value | |
202 | #undef TARGET_LIBCALL_VALUE | |
203 | #define TARGET_LIBCALL_VALUE cr16_libcall_value | |
204 | #undef TARGET_FUNCTION_VALUE_REGNO_P | |
205 | #define TARGET_FUNCTION_VALUE_REGNO_P cr16_function_value_regno_p | |
206 | ||
207 | /* printing the values. */ | |
208 | #undef TARGET_PRINT_OPERAND | |
209 | #define TARGET_PRINT_OPERAND cr16_print_operand | |
210 | #undef TARGET_PRINT_OPERAND_ADDRESS | |
211 | #define TARGET_PRINT_OPERAND_ADDRESS cr16_print_operand_address | |
212 | ||
213 | /* Relative costs of operations. */ | |
214 | #undef TARGET_ADDRESS_COST | |
215 | #define TARGET_ADDRESS_COST cr16_address_cost | |
216 | #undef TARGET_REGISTER_MOVE_COST | |
217 | #define TARGET_REGISTER_MOVE_COST cr16_register_move_cost | |
218 | #undef TARGET_MEMORY_MOVE_COST | |
219 | #define TARGET_MEMORY_MOVE_COST cr16_memory_move_cost | |
220 | ||
221 | /* Table of machine attributes. */ | |
222 | static const struct attribute_spec cr16_attribute_table[] = { | |
223 | /* ISRs have special prologue and epilogue requirements. */ | |
224 | /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler, | |
225 | affects_type_identity }. */ | |
226 | {"interrupt", 0, 0, false, true, true, NULL, false}, | |
227 | {NULL, 0, 0, false, false, false, NULL, false} | |
228 | }; | |
229 | ||
230 | /* TARGET_ASM_UNALIGNED_xx_OP generates .?byte directive | |
231 | .?byte directive along with @c is not understood by assembler. | |
232 | Therefore, make all TARGET_ASM_UNALIGNED_xx_OP same | |
233 | as TARGET_ASM_ALIGNED_xx_OP. */ | |
234 | #undef TARGET_ASM_UNALIGNED_HI_OP | |
235 | #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP | |
236 | #undef TARGET_ASM_UNALIGNED_SI_OP | |
237 | #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP | |
238 | #undef TARGET_ASM_UNALIGNED_DI_OP | |
239 | #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP | |
240 | ||
241 | /* Target hook implementations. */ | |
242 | ||
243 | /* Implements hook TARGET_RETURN_IN_MEMORY. */ | |
244 | static bool | |
245 | cr16_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED) | |
246 | { | |
247 | const HOST_WIDE_INT size = int_size_in_bytes (type); | |
248 | return ((size == -1) || (size > 8)); | |
249 | } | |
250 | ||
251 | /* Implement TARGET_CLASS_LIKELY_SPILLED_P. */ | |
252 | static bool | |
253 | cr16_class_likely_spilled_p (reg_class_t rclass) | |
254 | { | |
255 | if ((rclass) == SHORT_REGS || (rclass) == DOUBLE_BASE_REGS | |
256 | || (rclass) == LONG_REGS || (rclass) == GENERAL_REGS) | |
257 | return true; | |
258 | ||
259 | return false; | |
260 | } | |
261 | ||
262 | static int | |
263 | cr16_return_pops_args (tree fundecl ATTRIBUTE_UNUSED, | |
264 | tree funtype ATTRIBUTE_UNUSED, | |
265 | int size ATTRIBUTE_UNUSED) | |
266 | { | |
267 | return 0; | |
268 | } | |
269 | ||
270 | /* Returns true if data model selected via command line option | |
271 | is same as function argument. */ | |
272 | bool | |
273 | cr16_is_data_model (enum data_model_type model) | |
274 | { | |
275 | return (model == data_model); | |
276 | } | |
277 | ||
278 | /* Parse relevant options and override. */ | |
279 | static void | |
280 | cr16_override_options (void) | |
281 | { | |
282 | /* Disable -fdelete-null-pointer-checks option for CR16 target. | |
283 | Programs which rely on NULL pointer dereferences _not_ halting the | |
284 | program may not work properly with this option. So disable this | |
285 | option. */ | |
286 | flag_delete_null_pointer_checks = 0; | |
287 | ||
288 | /* FIXME: To avoid spill_failure ICE during exception handling, | |
289 | * disable cse_fllow_jumps. The spill error occurs when compiler | |
290 | * can't find a suitable candidate in GENERAL_REGS class to reload | |
291 | * a 32bit register. | |
292 | * Need to find a better way of avoiding this situation. */ | |
293 | if (flag_exceptions) | |
294 | flag_cse_follow_jumps = 0; | |
295 | ||
296 | /* If -fpic option, data_model == DM_FAR. */ | |
297 | if (flag_pic == NEAR_PIC) | |
298 | { | |
299 | data_model = DM_FAR; | |
300 | } | |
301 | ||
302 | /* The only option we want to examine is data model option. */ | |
303 | if (cr16_data_model) | |
304 | { | |
305 | if (strcmp (cr16_data_model, "medium") == 0) | |
306 | data_model = DM_DEFAULT; | |
307 | else if (strcmp (cr16_data_model, "near") == 0) | |
308 | data_model = DM_NEAR; | |
309 | else if (strcmp (cr16_data_model, "far") == 0) | |
310 | { | |
311 | if (TARGET_CR16CP) | |
312 | data_model = DM_FAR; | |
313 | else | |
314 | error ("data-model=far not valid for cr16c architecture"); | |
315 | } | |
316 | else | |
317 | error ("invalid data model option -mdata-model=%s", cr16_data_model); | |
318 | } | |
319 | else | |
320 | data_model = DM_DEFAULT; | |
321 | } | |
322 | ||
323 | /* Implements the macro TARGET_CONDITIONAL_REGISTER_USAGE. */ | |
324 | static void | |
325 | cr16_conditional_register_usage (void) | |
326 | { | |
327 | if (flag_pic) | |
328 | { | |
329 | fixed_regs[12] = call_used_regs[12] = 1; | |
330 | } | |
331 | } | |
332 | ||
333 | /* Stack layout and calling conventions routines. */ | |
334 | ||
335 | /* Return nonzero if the current function being compiled is an interrupt | |
336 | function as specified by the "interrupt" attribute. */ | |
337 | int | |
338 | cr16_interrupt_function_p (void) | |
339 | { | |
340 | tree attributes; | |
341 | ||
342 | attributes = TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl)); | |
343 | return (lookup_attribute ("interrupt", attributes) != NULL_TREE); | |
344 | } | |
345 | ||
346 | /* Compute values for the array current_frame_info.save_regs and the variable | |
347 | current_frame_info.reg_size. The index of current_frame_info.save_regs | |
348 | is numbers of register, each will get 1 if we need to save it in the | |
349 | current function, 0 if not. current_frame_info.reg_size is the total sum | |
350 | of the registers being saved. */ | |
351 | static void | |
352 | cr16_compute_save_regs (void) | |
353 | { | |
354 | unsigned int regno; | |
355 | ||
356 | /* Initialize here so in case the function is no-return it will be -1. */ | |
357 | current_frame_info.last_reg_to_save = -1; | |
358 | ||
359 | /* Initialize the number of bytes to be saved. */ | |
360 | current_frame_info.reg_size = 0; | |
361 | ||
362 | /* No need to save any registers if the function never returns. */ | |
363 | if (FUNC_IS_NORETURN_P (current_function_decl) && !MUST_SAVE_REGS_P ()) | |
364 | return; | |
365 | ||
366 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
367 | { | |
368 | if (fixed_regs[regno]) | |
369 | { | |
370 | current_frame_info.save_regs[regno] = 0; | |
371 | continue; | |
372 | } | |
373 | ||
374 | /* If this reg is used and not call-used (except RA), save it. */ | |
375 | if (cr16_interrupt_function_p ()) | |
376 | { | |
d5bf7b64 | 377 | if (!crtl->is_leaf && call_used_regs[regno]) |
b9fdd12b | 378 | /* This is a volatile reg in a non-leaf interrupt routine - save |
379 | it for the sake of its sons. */ | |
380 | current_frame_info.save_regs[regno] = 1; | |
381 | else if (df_regs_ever_live_p (regno)) | |
382 | /* This reg is used - save it. */ | |
383 | current_frame_info.save_regs[regno] = 1; | |
384 | else | |
385 | /* This reg is not used, and is not a volatile - don't save. */ | |
386 | current_frame_info.save_regs[regno] = 0; | |
387 | } | |
388 | else | |
389 | { | |
390 | /* If this reg is used and not call-used (except RA), save it. */ | |
391 | if (df_regs_ever_live_p (regno) | |
392 | && (!call_used_regs[regno] || regno == RETURN_ADDRESS_REGNUM)) | |
393 | current_frame_info.save_regs[regno] = 1; | |
394 | else | |
395 | current_frame_info.save_regs[regno] = 0; | |
396 | } | |
397 | } | |
398 | ||
399 | /* Save registers so the exception handler can modify them. */ | |
400 | if (crtl->calls_eh_return) | |
401 | { | |
402 | unsigned int i; | |
403 | ||
404 | for (i = 0;; ++i) | |
405 | { | |
406 | regno = EH_RETURN_DATA_REGNO (i); | |
407 | if (INVALID_REGNUM == regno) | |
408 | break; | |
409 | current_frame_info.save_regs[regno] = 1; | |
410 | } | |
411 | } | |
412 | ||
413 | for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++) | |
414 | if (current_frame_info.save_regs[regno] == 1) | |
415 | { | |
416 | current_frame_info.last_reg_to_save = regno; | |
417 | if (regno >= CR16_FIRST_DWORD_REGISTER) | |
418 | current_frame_info.reg_size += CR16_UNITS_PER_DWORD; | |
419 | else | |
420 | current_frame_info.reg_size += UNITS_PER_WORD; | |
421 | } | |
422 | } | |
423 | ||
424 | /* Compute the size of the local area and the size to be adjusted by the | |
425 | prologue and epilogue. */ | |
426 | static void | |
427 | cr16_compute_frame (void) | |
428 | { | |
429 | /* For aligning the local variables. */ | |
430 | int stack_alignment = STACK_BOUNDARY / BITS_PER_UNIT; | |
431 | int padding_locals; | |
432 | ||
433 | /* Padding needed for each element of the frame. */ | |
434 | current_frame_info.var_size = get_frame_size (); | |
435 | ||
436 | /* Align to the stack alignment. */ | |
437 | padding_locals = current_frame_info.var_size % stack_alignment; | |
438 | if (padding_locals) | |
439 | padding_locals = stack_alignment - padding_locals; | |
440 | ||
441 | current_frame_info.var_size += padding_locals; | |
442 | current_frame_info.total_size = current_frame_info.var_size | |
443 | + (ACCUMULATE_OUTGOING_ARGS | |
444 | ? crtl->outgoing_args_size : 0); | |
445 | } | |
446 | ||
447 | /* Implements the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */ | |
448 | int | |
449 | cr16_initial_elimination_offset (int from, int to) | |
450 | { | |
451 | /* Compute this since we need to use current_frame_info.reg_size. */ | |
452 | cr16_compute_save_regs (); | |
453 | ||
454 | /* Compute this since we need to use current_frame_info.var_size. */ | |
455 | cr16_compute_frame (); | |
456 | ||
457 | if (((from) == FRAME_POINTER_REGNUM) && ((to) == STACK_POINTER_REGNUM)) | |
458 | return (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0); | |
459 | else if (((from) == ARG_POINTER_REGNUM) && ((to) == FRAME_POINTER_REGNUM)) | |
460 | return (current_frame_info.reg_size + current_frame_info.var_size); | |
461 | else if (((from) == ARG_POINTER_REGNUM) && ((to) == STACK_POINTER_REGNUM)) | |
462 | return (current_frame_info.reg_size + current_frame_info.var_size | |
463 | + (ACCUMULATE_OUTGOING_ARGS ? crtl->outgoing_args_size : 0)); | |
464 | else | |
465 | gcc_unreachable (); | |
466 | } | |
467 | ||
468 | /* Register Usage. */ | |
469 | ||
470 | /* Return the class number of the smallest class containing reg number REGNO. | |
471 | This could be a conditional expression or could index an array. */ | |
472 | enum reg_class | |
473 | cr16_regno_reg_class (int regno) | |
474 | { | |
475 | if ((regno >= 0) && (regno < CR16_FIRST_DWORD_REGISTER)) | |
476 | return SHORT_REGS; | |
477 | ||
478 | if ((regno >= CR16_FIRST_DWORD_REGISTER) && (regno < FIRST_PSEUDO_REGISTER)) | |
479 | return LONG_REGS; | |
480 | ||
481 | return NO_REGS; | |
482 | } | |
483 | ||
484 | /* Return 1 if hard register REGNO can hold a value of machine-mode MODE. */ | |
485 | int | |
3754d046 | 486 | cr16_hard_regno_mode_ok (int regno, machine_mode mode) |
b9fdd12b | 487 | { |
488 | if ((GET_MODE_SIZE (mode) >= 4) && (regno == 11)) | |
489 | return 0; | |
490 | ||
491 | if (mode == DImode || mode == DFmode) | |
492 | { | |
493 | if ((regno > 8) || (regno & 1)) | |
494 | return 0; | |
495 | return 1; | |
496 | } | |
497 | ||
498 | if ((TARGET_INT32) | |
499 | && ((regno >= 12) && (GET_MODE_SIZE (mode) < 4 ))) | |
500 | return 0; | |
501 | ||
502 | /* CC can only hold CCmode values. */ | |
503 | if (GET_MODE_CLASS (mode) == MODE_CC) | |
504 | return 0; | |
505 | return 1; | |
506 | } | |
507 | ||
508 | /* Returns register number for function return value.*/ | |
509 | static inline unsigned int | |
510 | cr16_ret_register (void) | |
511 | { | |
512 | return 0; | |
513 | } | |
514 | ||
515 | /* Implements hook TARGET_STRUCT_VALUE_RTX. */ | |
516 | static rtx | |
517 | cr16_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED, | |
518 | int incoming ATTRIBUTE_UNUSED) | |
519 | { | |
520 | return gen_rtx_REG (Pmode, cr16_ret_register ()); | |
521 | } | |
522 | ||
523 | /* Returning function value. */ | |
524 | ||
525 | /* Worker function for TARGET_FUNCTION_VALUE_REGNO_P. */ | |
526 | static bool | |
527 | cr16_function_value_regno_p (const unsigned int regno) | |
528 | { | |
529 | return (regno == cr16_ret_register ()); | |
530 | } | |
531 | ||
532 | /* Create an RTX representing the place where a | |
533 | library function returns a value of mode MODE. */ | |
534 | static rtx | |
3754d046 | 535 | cr16_libcall_value (machine_mode mode, |
b9fdd12b | 536 | const_rtx func ATTRIBUTE_UNUSED) |
537 | { | |
538 | return gen_rtx_REG (mode, cr16_ret_register ()); | |
539 | } | |
540 | ||
541 | /* Create an RTX representing the place where a | |
542 | function returns a value of data type VALTYPE. */ | |
543 | static rtx | |
544 | cr16_function_value (const_tree type, | |
545 | const_tree fn_decl_or_type ATTRIBUTE_UNUSED, | |
546 | bool outgoing ATTRIBUTE_UNUSED) | |
547 | { | |
548 | return gen_rtx_REG (TYPE_MODE (type), cr16_ret_register ()); | |
549 | } | |
550 | ||
551 | /* Passing function arguments. */ | |
552 | ||
553 | /* If enough param regs are available for passing the param of type TYPE return | |
554 | the number of registers needed else 0. */ | |
555 | static int | |
556 | enough_regs_for_param (CUMULATIVE_ARGS * cum, const_tree type, | |
3754d046 | 557 | machine_mode mode) |
b9fdd12b | 558 | { |
559 | int type_size; | |
560 | int remaining_size; | |
561 | ||
562 | if (mode != BLKmode) | |
563 | type_size = GET_MODE_BITSIZE (mode); | |
564 | else | |
565 | type_size = int_size_in_bytes (type) * BITS_PER_UNIT; | |
566 | ||
567 | remaining_size = BITS_PER_WORD * (MAX_REG_FOR_PASSING_ARGS | |
568 | - (MIN_REG_FOR_PASSING_ARGS + cum->ints) + | |
569 | 1); | |
570 | ||
571 | /* Any variable which is too big to pass in two registers, will pass on | |
572 | stack. */ | |
573 | if ((remaining_size >= type_size) && (type_size <= 2 * BITS_PER_WORD)) | |
574 | return (type_size + BITS_PER_WORD - 1) / BITS_PER_WORD; | |
575 | ||
576 | return 0; | |
577 | } | |
578 | ||
579 | /* Implements the macro FUNCTION_ARG defined in cr16.h. */ | |
580 | static rtx | |
3754d046 | 581 | cr16_function_arg (cumulative_args_t cum_v, machine_mode mode, |
b9fdd12b | 582 | const_tree type, bool named ATTRIBUTE_UNUSED) |
583 | { | |
584 | CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v); | |
585 | cum->last_parm_in_reg = 0; | |
586 | ||
587 | /* function_arg () is called with this type just after all the args have | |
588 | had their registers assigned. The rtx that function_arg returns from | |
589 | this type is supposed to pass to 'gen_call' but currently it is not | |
590 | implemented (see macro GEN_CALL). */ | |
591 | if (type == void_type_node) | |
592 | return NULL_RTX; | |
593 | ||
594 | if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0)) | |
595 | return NULL_RTX; | |
596 | ||
597 | if (mode == BLKmode) | |
598 | { | |
599 | /* Enable structures that need padding bytes at the end to pass to a | |
600 | function in registers. */ | |
601 | if (enough_regs_for_param (cum, type, mode) != 0) | |
602 | { | |
603 | cum->last_parm_in_reg = 1; | |
604 | return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints); | |
605 | } | |
606 | } | |
607 | ||
608 | if ((MIN_REG_FOR_PASSING_ARGS + cum->ints) > MAX_REG_FOR_PASSING_ARGS) | |
609 | return NULL_RTX; | |
610 | else | |
611 | { | |
612 | if (enough_regs_for_param (cum, type, mode) != 0) | |
613 | { | |
614 | cum->last_parm_in_reg = 1; | |
615 | return gen_rtx_REG (mode, MIN_REG_FOR_PASSING_ARGS + cum->ints); | |
616 | } | |
617 | } | |
618 | ||
619 | return NULL_RTX; | |
620 | } | |
621 | ||
622 | /* Implements the macro INIT_CUMULATIVE_ARGS defined in cr16.h. */ | |
623 | void | |
624 | cr16_init_cumulative_args (CUMULATIVE_ARGS * cum, tree fntype, | |
625 | rtx libfunc ATTRIBUTE_UNUSED) | |
626 | { | |
627 | tree param, next_param; | |
628 | ||
629 | cum->ints = 0; | |
630 | ||
631 | /* Determine if this function has variable arguments. This is indicated by | |
632 | the last argument being 'void_type_mode' if there are no variable | |
633 | arguments. Change here for a different vararg. */ | |
634 | for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0; | |
635 | param != NULL_TREE; param = next_param) | |
636 | { | |
637 | next_param = TREE_CHAIN (param); | |
638 | if ((next_param == NULL_TREE) && (TREE_VALUE (param) != void_type_node)) | |
639 | { | |
640 | cum->ints = -1; | |
641 | return; | |
642 | } | |
643 | } | |
644 | } | |
645 | ||
646 | /* Implements the macro FUNCTION_ARG_ADVANCE defined in cr16.h. */ | |
647 | static void | |
3754d046 | 648 | cr16_function_arg_advance (cumulative_args_t cum_v, machine_mode mode, |
b9fdd12b | 649 | const_tree type, bool named ATTRIBUTE_UNUSED) |
650 | { | |
651 | CUMULATIVE_ARGS * cum = get_cumulative_args (cum_v); | |
652 | ||
653 | /* l holds the number of registers required. */ | |
654 | int l = GET_MODE_BITSIZE (mode) / BITS_PER_WORD; | |
655 | ||
656 | /* If the parameter isn't passed on a register don't advance cum. */ | |
657 | if (!cum->last_parm_in_reg) | |
658 | return; | |
659 | ||
660 | if (targetm.calls.must_pass_in_stack (mode, type) || (cum->ints < 0)) | |
661 | return; | |
662 | ||
663 | if ((mode == SImode) || (mode == HImode) | |
664 | || (mode == QImode) || (mode == DImode)) | |
665 | { | |
666 | if (l <= 1) | |
667 | cum->ints += 1; | |
668 | else | |
669 | cum->ints += l; | |
670 | } | |
671 | else if ((mode == SFmode) || (mode == DFmode)) | |
672 | cum->ints += l; | |
673 | else if ((mode) == BLKmode) | |
674 | { | |
675 | if ((l = enough_regs_for_param (cum, type, mode)) != 0) | |
676 | cum->ints += l; | |
677 | } | |
678 | return; | |
679 | } | |
680 | ||
681 | /* Implements the macro FUNCTION_ARG_REGNO_P defined in cr16.h. | |
682 | Return nonzero if N is a register used for passing parameters. */ | |
683 | int | |
684 | cr16_function_arg_regno_p (int n) | |
685 | { | |
686 | return ((n <= MAX_REG_FOR_PASSING_ARGS) && (n >= MIN_REG_FOR_PASSING_ARGS)); | |
687 | } | |
688 | ||
689 | /* Addressing modes. | |
690 | Following set of function implement the macro GO_IF_LEGITIMATE_ADDRESS | |
691 | defined in cr16.h. */ | |
692 | ||
693 | /* Helper function to check if is a valid base register that can | |
694 | hold address. */ | |
695 | static int | |
696 | cr16_addr_reg_p (rtx addr_reg) | |
697 | { | |
698 | rtx reg; | |
699 | ||
700 | if (REG_P (addr_reg)) | |
701 | reg = addr_reg; | |
702 | else if ((GET_CODE (addr_reg) == SUBREG) | |
703 | && REG_P (SUBREG_REG (addr_reg)) | |
704 | && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (addr_reg))) | |
705 | <= UNITS_PER_WORD)) | |
706 | reg = SUBREG_REG (addr_reg); | |
707 | else | |
708 | return FALSE; | |
709 | ||
710 | if (GET_MODE (reg) != Pmode) | |
711 | return FALSE; | |
712 | ||
713 | return TRUE; | |
714 | } | |
715 | ||
716 | /* Helper functions: Created specifically for decomposing operand of CONST | |
717 | Recursively look into expression x for code or data symbol. | |
718 | The function expects the expression to contain combination of | |
719 | SYMBOL_REF, CONST_INT, (PLUS or MINUS) | |
720 | LABEL_REF, CONST_INT, (PLUS or MINUS) | |
721 | SYMBOL_REF | |
722 | LABEL_REF | |
723 | All other combinations will result in code = -1 and data = ILLEGAL_DM | |
724 | code data | |
725 | -1 ILLEGAL_DM The expression did not contain SYMBOL_REF or LABEL_REF | |
726 | 0 DM_FAR SYMBOL_REF was found and it was far data reference. | |
727 | 0 DM_DEFAULT SYMBOL_REF was found and it was medium data reference. | |
728 | 1 ILLEGAL_DM LABEL_REF was found. | |
729 | 2 ILLEGAL_DM SYMBOL_REF was found and it was function reference. */ | |
730 | void | |
731 | cr16_decompose_const (rtx x, int *code, enum data_model_type *data, | |
732 | bool treat_as_const) | |
733 | { | |
734 | *code = -1; | |
735 | *data = ILLEGAL_DM; | |
736 | switch (GET_CODE (x)) | |
737 | { | |
738 | case SYMBOL_REF: | |
739 | *code = SYMBOL_REF_FUNCTION_P (x) ? 2 : 0; | |
740 | /* 2 indicates func sym. */ | |
741 | if (*code == 0) | |
742 | { | |
743 | if (CR16_TARGET_DATA_NEAR) | |
744 | *data = DM_DEFAULT; | |
745 | else if (CR16_TARGET_DATA_MEDIUM) | |
746 | *data = DM_FAR; | |
747 | else if (CR16_TARGET_DATA_FAR) | |
748 | { | |
749 | if (treat_as_const) | |
750 | /* This will be used only for printing | |
751 | the qualifier. This call is (may be) | |
752 | made by cr16_print_operand_address. */ | |
753 | *data = DM_FAR; | |
754 | else | |
755 | /* This call is (may be) made by | |
756 | cr16_legitimate_address_p. */ | |
757 | *data = ILLEGAL_DM; | |
758 | } | |
759 | } | |
760 | return; | |
761 | ||
762 | case LABEL_REF: | |
763 | /* 1 - indicates non-function symbol. */ | |
764 | *code = 1; | |
765 | return; | |
766 | ||
767 | case PLUS: | |
768 | case MINUS: | |
769 | /* Look into the tree nodes. */ | |
770 | if (GET_CODE (XEXP (x, 0)) == CONST_INT) | |
771 | cr16_decompose_const (XEXP (x, 1), code, data, treat_as_const); | |
772 | else if (GET_CODE (XEXP (x, 1)) == CONST_INT) | |
773 | cr16_decompose_const (XEXP (x, 0), code, data, treat_as_const); | |
774 | return; | |
775 | default: | |
776 | return; | |
777 | } | |
778 | } | |
779 | ||
780 | /* Decompose Address | |
781 | This function decomposes the address returns the type of address | |
782 | as defined in enum cr16_addrtype. It also fills the parameter *out. | |
783 | The decomposed address can be used for two purposes. One to | |
784 | check if the address is valid and second to print the address | |
785 | operand. | |
786 | ||
787 | Following tables list valid address supported in CR16C/C+ architectures. | |
788 | Legend: | |
789 | aN : Absoulte address N-bit address | |
790 | R : One 16-bit register | |
791 | RP : Consecutive two 16-bit registers or one 32-bit register | |
792 | I : One 32-bit register | |
793 | dispN : Signed displacement of N-bits | |
794 | ||
795 | ----Code addresses---- | |
796 | Branch operands: | |
797 | disp9 : CR16_ABSOLUTE (disp) | |
798 | disp17 : CR16_ABSOLUTE (disp) | |
799 | disp25 : CR16_ABSOLUTE (disp) | |
800 | RP + disp25 : CR16_REGP_REL (base, disp) | |
801 | ||
802 | Jump operands: | |
803 | RP : CR16_REGP_REL (base, disp=0) | |
804 | a24 : CR16_ABSOLUTE (disp) | |
805 | ||
806 | ----Data addresses---- | |
807 | a20 : CR16_ABSOLUTE (disp) near (1M) | |
808 | a24 : CR16_ABSOLUTE (disp) medium (16M) | |
809 | R + d20 : CR16_REG_REL (base, disp) near (1M+64K) | |
810 | RP + d4 : CR16_REGP_REL (base, disp) far (4G) | |
811 | RP + d16 : CR16_REGP_REL (base, disp) far (4G) | |
812 | RP + d20 : CR16_REGP_REL (base, disp) far (4G) | |
813 | I : *** Valid but port does not support this | |
814 | I + a20 : *** Valid but port does not support this | |
815 | I + RP + d14: CR16_INDEX_REGP_REL (base, index, disp) far (4G) | |
816 | I + RP + d20: CR16_INDEX_REGP_REL (base, index, disp) far (4G) | |
817 | ||
818 | Decomposing Data model in case of absolute address. | |
819 | ||
820 | Target Option Address type Resultant Data ref type | |
821 | ---------------------- ------------ ----------------------- | |
822 | CR16_TARGET_MODEL_NEAR ABS20 DM_DEFAULT | |
823 | CR16_TARGET_MODEL_NEAR IMM20 DM_DEFAULT | |
824 | CR16_TARGET_MODEL_NEAR ABS24 Invalid | |
825 | CR16_TARGET_MODEL_NEAR IMM32 Invalid | |
826 | ||
827 | CR16_TARGET_MODEL_MEDIUM ABS20 DM_DEFAULT | |
828 | CR16_TARGET_MODEL_MEDIUM IMM20 DM_DEFAULT | |
829 | CR16_TARGET_MODEL_MEDIUM ABS24 DM_FAR | |
830 | CR16_TARGET_MODEL_MEDIUM IMM32 Invalid | |
831 | ||
832 | CR16_TARGET_MODEL_FAR ABS20 DM_DEFAULT | |
833 | CR16_TARGET_MODEL_FAR IMM20 DM_DEFAULT | |
834 | CR16_TARGET_MODEL_FAR ABS24 DM_FAR | |
835 | CR16_TARGET_MODEL_FAR IMM32 DM_FAR. */ | |
836 | enum cr16_addrtype | |
837 | cr16_decompose_address (rtx addr, struct cr16_address *out, | |
838 | bool debug_print, bool treat_as_const) | |
839 | { | |
840 | rtx base = NULL_RTX, disp = NULL_RTX, index = NULL_RTX; | |
841 | enum data_model_type data = ILLEGAL_DM; | |
842 | int code = -1; | |
843 | enum cr16_addrtype retval = CR16_INVALID; | |
844 | ||
845 | switch (GET_CODE (addr)) | |
846 | { | |
847 | case CONST_INT: | |
848 | /* Absolute address (known at compile time). */ | |
849 | code = 0; | |
850 | if (debug_print) | |
851 | fprintf (stderr, "\ncode:%d", code); | |
852 | disp = addr; | |
853 | ||
854 | if (debug_print) | |
855 | { | |
856 | fprintf (stderr, "\ndisp:"); | |
857 | debug_rtx (disp); | |
858 | } | |
859 | ||
860 | if (UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 20)) | |
861 | { | |
862 | data = DM_DEFAULT; | |
863 | if (debug_print) | |
864 | fprintf (stderr, "\ndata:%d", data); | |
865 | retval = CR16_ABSOLUTE; | |
866 | } | |
867 | else if (UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 24)) | |
868 | { | |
869 | if (!CR16_TARGET_DATA_NEAR) | |
870 | { | |
871 | data = DM_FAR; | |
872 | if (debug_print) | |
873 | fprintf (stderr, "\ndata:%d", data); | |
874 | retval = CR16_ABSOLUTE; | |
875 | } | |
876 | else | |
877 | return CR16_INVALID; /* ABS24 is not support in NEAR model. */ | |
878 | } | |
879 | else | |
880 | return CR16_INVALID; | |
881 | break; | |
882 | ||
883 | case CONST: | |
884 | /* A CONST is an expression of PLUS or MINUS with | |
885 | CONST_INT, SYMBOL_REF or LABEL_REF. This is the | |
886 | result of assembly-time arithmetic computation. */ | |
887 | retval = CR16_ABSOLUTE; | |
888 | disp = addr; | |
889 | /* Call the helper function to check the validity. */ | |
890 | cr16_decompose_const (XEXP (addr, 0), &code, &data, treat_as_const); | |
891 | if ((code == 0) && (data == ILLEGAL_DM)) | |
892 | /* CONST is not valid code or data address. */ | |
893 | return CR16_INVALID; | |
894 | if (debug_print) | |
895 | { | |
896 | fprintf (stderr, "\ndisp:"); | |
897 | debug_rtx (disp); | |
898 | fprintf (stderr, "\ncode:%d", code); | |
899 | fprintf (stderr, "\ndata:%d", data); | |
900 | } | |
901 | break; | |
902 | ||
903 | case LABEL_REF: | |
904 | retval = CR16_ABSOLUTE; | |
905 | disp = addr; | |
906 | /* 1 - indicates non-function symbol. */ | |
907 | code = 1; | |
908 | if (debug_print) | |
909 | { | |
910 | fprintf (stderr, "\ndisp:"); | |
911 | debug_rtx (disp); | |
912 | fprintf (stderr, "\ncode:%d", code); | |
913 | } | |
914 | break; | |
915 | ||
916 | case SYMBOL_REF: | |
917 | /* Absolute address (known at link time). */ | |
918 | retval = CR16_ABSOLUTE; | |
919 | disp = addr; | |
920 | /* This is a code address if symbol_ref is a function. */ | |
921 | /* 2 indicates func sym. */ | |
922 | code = SYMBOL_REF_FUNCTION_P (addr) ? 2 : 0; | |
923 | if (debug_print) | |
924 | { | |
925 | fprintf (stderr, "\ndisp:"); | |
926 | debug_rtx (disp); | |
927 | fprintf (stderr, "\ncode:%d", code); | |
928 | } | |
929 | /* If not function ref then check if valid data ref. */ | |
930 | if (code == 0) | |
931 | { | |
932 | if (CR16_TARGET_DATA_NEAR) | |
933 | data = DM_DEFAULT; | |
934 | else if (CR16_TARGET_DATA_MEDIUM) | |
935 | data = DM_FAR; | |
936 | else if (CR16_TARGET_DATA_FAR) | |
937 | { | |
938 | if (treat_as_const) | |
939 | /* This will be used only for printing the | |
940 | qualifier. This call is (may be) made | |
941 | by cr16_print_operand_address. */ | |
942 | data = DM_FAR; | |
943 | else | |
944 | /* This call is (may be) made by | |
945 | cr16_legitimate_address_p. */ | |
946 | return CR16_INVALID; | |
947 | } | |
948 | else | |
949 | data = DM_DEFAULT; | |
950 | } | |
951 | if (debug_print) | |
952 | fprintf (stderr, "\ndata:%d", data); | |
953 | break; | |
954 | ||
955 | case REG: | |
956 | case SUBREG: | |
957 | /* Register relative address. */ | |
958 | /* Assume REG fits in a single register. */ | |
959 | retval = CR16_REG_REL; | |
960 | if (GET_MODE_BITSIZE (GET_MODE (addr)) > BITS_PER_WORD) | |
961 | if (!LONG_REG_P (REGNO (addr))) | |
962 | /* REG will result in reg pair. */ | |
963 | retval = CR16_REGP_REL; | |
964 | base = addr; | |
965 | if (debug_print) | |
966 | { | |
967 | fprintf (stderr, "\nbase:"); | |
968 | debug_rtx (base); | |
969 | } | |
970 | break; | |
971 | ||
972 | case PLUS: | |
973 | switch (GET_CODE (XEXP (addr, 0))) | |
974 | { | |
975 | case REG: | |
976 | case SUBREG: | |
977 | /* REG + DISP20. */ | |
978 | /* All Reg relative addresses having a displacement needs | |
979 | to fit in 20-bits. */ | |
980 | disp = XEXP (addr, 1); | |
981 | if (debug_print) | |
982 | { | |
983 | fprintf (stderr, "\ndisp:"); | |
984 | debug_rtx (disp); | |
985 | } | |
986 | switch (GET_CODE (XEXP (addr, 1))) | |
987 | { | |
988 | case CONST_INT: | |
989 | /* Shall fit in 20-bits. */ | |
990 | if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 20)) | |
991 | return CR16_INVALID; | |
992 | code = 0; | |
993 | if (debug_print) | |
994 | fprintf (stderr, "\ncode:%d", code); | |
995 | break; | |
996 | ||
997 | case UNSPEC: | |
998 | switch (XINT (XEXP (addr, 1), 1)) | |
999 | { | |
1000 | case UNSPEC_LIBRARY_OFFSET: | |
1001 | default: | |
1002 | gcc_unreachable (); | |
1003 | } | |
1004 | break; | |
1005 | ||
1006 | case LABEL_REF: | |
1007 | case SYMBOL_REF: | |
1008 | case CONST: | |
1009 | /* This is also a valid expression for address. | |
1010 | However, we cannot ascertain if the resultant | |
1011 | displacement will be valid 20-bit value. Therefore, | |
1012 | lets not allow such an expression for now. This will | |
1013 | be updated when we find a way to validate this | |
1014 | expression as legitimate address. | |
1015 | Till then fall through CR16_INVALID. */ | |
1016 | default: | |
1017 | return CR16_INVALID; | |
1018 | } | |
1019 | ||
1020 | /* Now check if REG can fit into single or pair regs. */ | |
1021 | retval = CR16_REG_REL; | |
1022 | base = XEXP (addr, 0); | |
1023 | if (debug_print) | |
1024 | { | |
1025 | fprintf (stderr, "\nbase:"); | |
1026 | debug_rtx (base); | |
1027 | } | |
1028 | if (GET_MODE_BITSIZE (GET_MODE ((XEXP (addr, 0)))) > BITS_PER_WORD) | |
1029 | { | |
1030 | if (!LONG_REG_P (REGNO ((XEXP (addr, 0))))) | |
1031 | /* REG will result in reg pair. */ | |
1032 | retval = CR16_REGP_REL; | |
1033 | } | |
1034 | break; | |
1035 | ||
1036 | case PLUS: | |
1037 | /* Valid expr: | |
1038 | plus | |
1039 | /\ | |
1040 | / \ | |
1041 | plus idx | |
1042 | /\ | |
1043 | / \ | |
1044 | reg const_int | |
1045 | ||
1046 | Check if the operand 1 is valid index register. */ | |
1047 | data = ILLEGAL_DM; | |
1048 | if (debug_print) | |
1049 | fprintf (stderr, "\ndata:%d", data); | |
1050 | switch (GET_CODE (XEXP (addr, 1))) | |
1051 | { | |
1052 | case REG: | |
1053 | case SUBREG: | |
1054 | if (!REG_OK_FOR_INDEX_P (XEXP (addr, 1))) | |
1055 | return CR16_INVALID; | |
1056 | /* OK. REG is a valid index register. */ | |
1057 | index = XEXP (addr, 1); | |
1058 | if (debug_print) | |
1059 | { | |
1060 | fprintf (stderr, "\nindex:"); | |
1061 | debug_rtx (index); | |
1062 | } | |
1063 | break; | |
1064 | default: | |
1065 | return CR16_INVALID; | |
1066 | } | |
1067 | /* Check if operand 0 of operand 0 is REGP. */ | |
1068 | switch (GET_CODE (XEXP (XEXP (addr, 0), 0))) | |
1069 | { | |
1070 | case REG: | |
1071 | case SUBREG: | |
1072 | /* Now check if REG is a REGP and not in LONG regs. */ | |
1073 | if (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (addr, 0), 0))) | |
1074 | > BITS_PER_WORD) | |
1075 | { | |
1076 | if (REGNO (XEXP (XEXP (addr, 0), 0)) | |
1077 | >= CR16_FIRST_DWORD_REGISTER) | |
1078 | return CR16_INVALID; | |
1079 | base = XEXP (XEXP (addr, 0), 0); | |
1080 | if (debug_print) | |
1081 | { | |
1082 | fprintf (stderr, "\nbase:"); | |
1083 | debug_rtx (base); | |
1084 | } | |
1085 | } | |
1086 | else | |
1087 | return CR16_INVALID; | |
1088 | break; | |
1089 | default: | |
1090 | return CR16_INVALID; | |
1091 | } | |
1092 | /* Now check if the operand 1 of operand 0 is const_int. */ | |
1093 | if (GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST_INT) | |
1094 | { | |
1095 | disp = XEXP (XEXP (addr, 0), 1); | |
1096 | if (debug_print) | |
1097 | { | |
1098 | fprintf (stderr, "\ndisp:"); | |
1099 | debug_rtx (disp); | |
1100 | } | |
1101 | if (!UNSIGNED_INT_FITS_N_BITS (INTVAL (disp), 20)) | |
1102 | return CR16_INVALID; | |
1103 | } | |
1104 | else | |
1105 | return CR16_INVALID; | |
1106 | retval = CR16_INDEX_REGP_REL; | |
1107 | break; | |
1108 | default: | |
1109 | return CR16_INVALID; | |
1110 | } | |
1111 | break; | |
1112 | ||
1113 | default: | |
1114 | return CR16_INVALID; | |
1115 | } | |
1116 | ||
1117 | /* Check if the base and index registers are valid. */ | |
1118 | if (base && !(cr16_addr_reg_p (base))) | |
1119 | return CR16_INVALID; | |
1120 | if (base && !(CR16_REG_OK_FOR_BASE_P (base))) | |
1121 | return CR16_INVALID; | |
1122 | if (index && !(REG_OK_FOR_INDEX_P (index))) | |
1123 | return CR16_INVALID; | |
1124 | ||
1125 | /* Write the decomposition to out parameter. */ | |
1126 | out->base = base; | |
1127 | out->disp = disp; | |
1128 | out->index = index; | |
1129 | out->data = data; | |
1130 | out->code = code; | |
1131 | ||
1132 | return retval; | |
1133 | } | |
1134 | ||
1135 | /* Return non-zero value if 'x' is legitimate PIC operand | |
1136 | when generating PIC code. */ | |
1137 | int | |
1138 | legitimate_pic_operand_p (rtx x) | |
1139 | { | |
1140 | switch (GET_CODE (x)) | |
1141 | { | |
1142 | case SYMBOL_REF: | |
1143 | return 0; | |
1144 | break; | |
1145 | case LABEL_REF: | |
1146 | return 0; | |
1147 | break; | |
1148 | case CONST: | |
1149 | /* REVISIT: Use something like symbol_referenced_p. */ | |
1150 | if (GET_CODE (XEXP (x, 0)) == PLUS | |
1151 | && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF | |
1152 | || GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF) | |
1153 | && (GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)) | |
1154 | return 0; | |
1155 | break; | |
1156 | case MEM: | |
1157 | return legitimate_pic_operand_p (XEXP (x, 0)); | |
1158 | break; | |
1159 | default: | |
1160 | break; | |
1161 | } | |
1162 | return 1; | |
1163 | } | |
1164 | ||
1165 | /* Convert a non-PIC address in `orig' to a PIC address in `reg'. | |
1166 | ||
1167 | Input Output (-f pic) Output (-f PIC) | |
1168 | orig reg | |
1169 | ||
1170 | C1 symbol symbol@BRO (r12) symbol@GOT (r12) | |
1171 | ||
1172 | C2 symbol + offset symbol+offset@BRO (r12) symbol+offset@GOT (r12) | |
1173 | ||
1174 | NOTE: @BRO is added using unspec:BRO | |
1175 | NOTE: @GOT is added using unspec:GOT. */ | |
1176 | rtx | |
3754d046 | 1177 | legitimize_pic_address (rtx orig, machine_mode mode ATTRIBUTE_UNUSED, |
b9fdd12b | 1178 | rtx reg) |
1179 | { | |
1180 | /* First handle a simple SYMBOL_REF or LABEL_REF. */ | |
1181 | if (GET_CODE (orig) == SYMBOL_REF || GET_CODE (orig) == LABEL_REF) | |
1182 | { | |
1183 | if (reg == 0) | |
1184 | reg = gen_reg_rtx (Pmode); | |
1185 | ||
1186 | if (flag_pic == NEAR_PIC) | |
1187 | { | |
1188 | /* Unspec to handle -fpic option. */ | |
1189 | emit_insn (gen_unspec_bro_addr (reg, orig)); | |
1190 | emit_insn (gen_addsi3 (reg, reg, pic_offset_table_rtx)); | |
1191 | } | |
1192 | else if (flag_pic == FAR_PIC) | |
1193 | { | |
1194 | /* Unspec to handle -fPIC option. */ | |
1195 | emit_insn (gen_unspec_got_addr (reg, orig)); | |
1196 | } | |
1197 | return reg; | |
1198 | } | |
1199 | else if (GET_CODE (orig) == CONST) | |
1200 | { | |
1201 | /* To handle (symbol + offset). */ | |
1202 | rtx base, offset; | |
1203 | ||
1204 | if (GET_CODE (XEXP (orig, 0)) == PLUS | |
1205 | && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx) | |
1206 | return orig; | |
1207 | ||
1208 | if (reg == 0) | |
1209 | { | |
1210 | gcc_assert (can_create_pseudo_p ()); | |
1211 | reg = gen_reg_rtx (Pmode); | |
1212 | } | |
1213 | ||
1214 | gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS); | |
1215 | ||
1216 | base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg); | |
1217 | offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode, | |
1218 | base == reg ? 0 : reg); | |
1219 | ||
1220 | /* REVISIT: Optimize for const-offsets. */ | |
1221 | emit_insn (gen_addsi3 (reg, base, offset)); | |
1222 | ||
1223 | return reg; | |
1224 | } | |
1225 | return orig; | |
1226 | } | |
1227 | ||
1228 | /* Implementation of TARGET_LEGITIMATE_ADDRESS_P. */ | |
1229 | static bool | |
3754d046 | 1230 | cr16_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED, |
b9fdd12b | 1231 | rtx addr, bool strict) |
1232 | { | |
1233 | enum cr16_addrtype addrtype; | |
1234 | struct cr16_address address; | |
1235 | ||
1236 | if (TARGET_DEBUG_ADDR) | |
1237 | { | |
1238 | fprintf (stderr, | |
1239 | "\n======\nTARGET_LEGITIMATE_ADDRESS_P, mode = %s, strict = %d", | |
1240 | GET_MODE_NAME (mode), strict); | |
1241 | debug_rtx (addr); | |
1242 | } | |
1243 | addrtype = cr16_decompose_address (addr, &address, | |
1244 | (TARGET_DEBUG_ADDR ? 1 : 0), FALSE); | |
1245 | ||
1246 | if (TARGET_DEBUG_ADDR) | |
1247 | { | |
1248 | const char *typestr; | |
1249 | ||
1250 | switch (addrtype) | |
1251 | { | |
1252 | case CR16_INVALID: | |
1253 | typestr = "invalid"; | |
1254 | break; | |
1255 | case CR16_ABSOLUTE: | |
1256 | typestr = "absolute"; | |
1257 | break; | |
1258 | case CR16_REG_REL: | |
1259 | typestr = "register relative"; | |
1260 | break; | |
1261 | case CR16_REGP_REL: | |
1262 | typestr = "register pair relative"; | |
1263 | break; | |
1264 | case CR16_INDEX_REGP_REL: | |
1265 | typestr = "index + register pair relative"; | |
1266 | break; | |
1267 | default: | |
1268 | gcc_unreachable (); | |
1269 | } | |
1270 | fprintf (stderr, "\ncr16 address type: %s\n", typestr); | |
1271 | } | |
1272 | ||
1273 | if (addrtype == CR16_INVALID) | |
1274 | return FALSE; | |
1275 | ||
1276 | if (strict) | |
1277 | { | |
1278 | if (address.base | |
1279 | && !REGNO_MODE_OK_FOR_BASE_P (REGNO (address.base), mode)) | |
1280 | { | |
1281 | if (TARGET_DEBUG_ADDR) | |
1282 | fprintf (stderr, "base register not strict\n"); | |
1283 | return FALSE; | |
1284 | } | |
1285 | if (address.index && !REGNO_OK_FOR_INDEX_P (REGNO (address.index))) | |
1286 | { | |
1287 | if (TARGET_DEBUG_ADDR) | |
1288 | fprintf (stderr, "index register not strict\n"); | |
1289 | return FALSE; | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | /* Return true if addressing mode is register relative. */ | |
1294 | if (flag_pic) | |
1295 | { | |
1296 | if (addrtype == CR16_REG_REL || addrtype == CR16_REGP_REL) | |
1297 | return TRUE; | |
1298 | else | |
1299 | return FALSE; | |
1300 | } | |
1301 | ||
1302 | return TRUE; | |
1303 | } | |
1304 | ||
1305 | /* Routines to compute costs. */ | |
1306 | ||
1307 | /* Return cost of the memory address x. */ | |
1308 | static int | |
3754d046 | 1309 | cr16_address_cost (rtx addr, machine_mode mode ATTRIBUTE_UNUSED, |
d9c5e5f4 | 1310 | addr_space_t as ATTRIBUTE_UNUSED, |
1311 | bool speed ATTRIBUTE_UNUSED) | |
b9fdd12b | 1312 | { |
1313 | enum cr16_addrtype addrtype; | |
1314 | struct cr16_address address; | |
1315 | int cost = 2; | |
1316 | ||
1317 | addrtype = cr16_decompose_address (addr, &address, 0, FALSE); | |
1318 | ||
1319 | gcc_assert (addrtype != CR16_INVALID); | |
1320 | ||
1321 | /* CR16_ABSOLUTE : 3 | |
1322 | CR16_REG_REL (disp !=0) : 4 | |
1323 | CR16_REG_REL (disp ==0) : 5 | |
1324 | CR16_REGP_REL (disp !=0) : 6 | |
1325 | CR16_REGP_REL (disp ==0) : 7 | |
1326 | CR16_INDEX_REGP_REL (disp !=0) : 8 | |
1327 | CR16_INDEX_REGP_REL (disp ==0) : 9. */ | |
1328 | switch (addrtype) | |
1329 | { | |
1330 | case CR16_ABSOLUTE: | |
1331 | cost += 1; | |
1332 | break; | |
1333 | case CR16_REGP_REL: | |
1334 | cost += 2; | |
1335 | /* Fall through. */ | |
1336 | case CR16_REG_REL: | |
1337 | cost += 3; | |
1338 | if (address.disp) | |
1339 | cost -= 1; | |
1340 | break; | |
1341 | case CR16_INDEX_REGP_REL: | |
1342 | cost += 7; | |
1343 | if (address.disp) | |
1344 | cost -= 1; | |
1345 | default: | |
1346 | break; | |
1347 | } | |
1348 | ||
1349 | if (TARGET_DEBUG_ADDR) | |
1350 | { | |
1351 | fprintf (stderr, "\n======\nmacro TARGET_ADDRESS_COST = %d\n", cost); | |
1352 | debug_rtx (addr); | |
1353 | } | |
1354 | ||
1355 | return cost; | |
1356 | } | |
1357 | ||
1358 | ||
1359 | /* Implement `TARGET_REGISTER_MOVE_COST'. */ | |
1360 | static int | |
3754d046 | 1361 | cr16_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED, |
b9fdd12b | 1362 | reg_class_t from ATTRIBUTE_UNUSED, reg_class_t to) |
1363 | { | |
1364 | return (to != GENERAL_REGS ? 8 : 2); | |
1365 | } | |
1366 | ||
1367 | /* Implement `TARGET_MEMORY_MOVE_COST'. */ | |
1368 | ||
1369 | /* Return the cost of moving data of mode MODE between a register of class | |
1370 | CLASS and memory; IN is zero if the value is to be written to memory, | |
1371 | nonzero if it is to be read in. This cost is relative to those in | |
1372 | REGISTER_MOVE_COST. */ | |
1373 | static int | |
3754d046 | 1374 | cr16_memory_move_cost (machine_mode mode, |
b9fdd12b | 1375 | reg_class_t rclass ATTRIBUTE_UNUSED, |
1376 | bool in ATTRIBUTE_UNUSED) | |
1377 | { | |
1378 | /* One LD or ST takes twice the time of a simple reg-reg move. */ | |
1379 | if (reg_classes_intersect_p (rclass, GENERAL_REGS)) | |
1380 | return (4 * HARD_REGNO_NREGS (0, mode)); | |
1381 | else | |
1382 | return (100); | |
1383 | } | |
1384 | ||
1385 | /* Instruction output. */ | |
1386 | ||
1387 | /* Check if a const_double is ok for cr16 store-immediate instructions. */ | |
1388 | int | |
1389 | cr16_const_double_ok (rtx op) | |
1390 | { | |
1391 | if (GET_MODE (op) == SFmode) | |
1392 | { | |
1393 | REAL_VALUE_TYPE r; | |
1394 | long l; | |
1395 | REAL_VALUE_FROM_CONST_DOUBLE (r, op); | |
1396 | REAL_VALUE_TO_TARGET_SINGLE (r, l); | |
1397 | return UNSIGNED_INT_FITS_N_BITS (l, 4) ? 1 : 0; | |
1398 | } | |
1399 | ||
1400 | return ((UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_LOW (op), 4)) && | |
1401 | (UNSIGNED_INT_FITS_N_BITS (CONST_DOUBLE_HIGH (op), 4))) ? 1 : 0; | |
1402 | } | |
1403 | ||
1404 | /* Returns bit position of first 0 or 1 bit. | |
1405 | It is safe to assume val as 16-bit wide. */ | |
1406 | int | |
1407 | cr16_operand_bit_pos (int val, int bitval) | |
1408 | { | |
1409 | int i; | |
1410 | if (bitval == 0) | |
1411 | val = ~val; | |
1412 | ||
1413 | for (i = 0; i < 16; i++) | |
1414 | if (val & (1 << i)) | |
1415 | break; | |
1416 | return i; | |
1417 | } | |
1418 | ||
1419 | /* Implements the macro PRINT_OPERAND defined in cr16.h. */ | |
1420 | static void | |
1421 | cr16_print_operand (FILE * file, rtx x, int code) | |
1422 | { | |
1423 | int ptr_dereference = 0; | |
1424 | ||
1425 | switch (code) | |
1426 | { | |
1427 | case 'd': | |
1428 | { | |
1429 | const char *cr16_cmp_str; | |
1430 | switch (GET_CODE (x)) | |
1431 | { | |
1432 | /* MD: compare (reg, reg or imm) but CR16: cmp (reg or imm, reg) | |
1433 | -> swap all non symmetric ops. */ | |
1434 | case EQ: | |
1435 | cr16_cmp_str = "eq"; | |
1436 | break; | |
1437 | case NE: | |
1438 | cr16_cmp_str = "ne"; | |
1439 | break; | |
1440 | case GT: | |
1441 | cr16_cmp_str = "lt"; | |
1442 | break; | |
1443 | case GTU: | |
1444 | cr16_cmp_str = "lo"; | |
1445 | break; | |
1446 | case LT: | |
1447 | cr16_cmp_str = "gt"; | |
1448 | break; | |
1449 | case LTU: | |
1450 | cr16_cmp_str = "hi"; | |
1451 | break; | |
1452 | case GE: | |
1453 | cr16_cmp_str = "le"; | |
1454 | break; | |
1455 | case GEU: | |
1456 | cr16_cmp_str = "ls"; | |
1457 | break; | |
1458 | case LE: | |
1459 | cr16_cmp_str = "ge"; | |
1460 | break; | |
1461 | case LEU: | |
1462 | cr16_cmp_str = "hs"; | |
1463 | break; | |
1464 | default: | |
1465 | gcc_unreachable (); | |
1466 | } | |
1467 | fprintf (file, "%s", cr16_cmp_str); | |
1468 | return; | |
1469 | } | |
1470 | case '$': | |
1471 | putc ('$', file); | |
1472 | return; | |
1473 | ||
1474 | case 'p': | |
1475 | if (GET_CODE (x) == REG) | |
1476 | { | |
1477 | /* For Push instructions, we should not print register pairs. */ | |
1478 | fprintf (file, "%s", reg_names[REGNO (x)]); | |
1479 | return; | |
1480 | } | |
1481 | break; | |
1482 | ||
1483 | case 'b': | |
1484 | /* Print the immediate address for bal | |
1485 | 'b' is used instead of 'a' to avoid compiler calling | |
1486 | the GO_IF_LEGITIMATE_ADDRESS which cannot | |
1487 | perform checks on const_int code addresses as it | |
1488 | assumes all const_int are data addresses. */ | |
1489 | fprintf (file, "0x%lx", INTVAL (x)); | |
1490 | return; | |
1491 | ||
1492 | case 'r': | |
1493 | /* Print bit position of first 0. */ | |
1494 | fprintf (file, "%d", cr16_operand_bit_pos (INTVAL (x), 0)); | |
1495 | return; | |
1496 | ||
1497 | case 's': | |
1498 | /* Print bit position of first 1. */ | |
1499 | fprintf (file, "%d", cr16_operand_bit_pos (INTVAL (x), 1)); | |
1500 | return; | |
1501 | case 'g': | |
1502 | /* 'g' is used for implicit mem: dereference. */ | |
1503 | ptr_dereference = 1; | |
1504 | case 'f': | |
1505 | case 0: | |
1506 | /* default. */ | |
1507 | switch (GET_CODE (x)) | |
1508 | { | |
1509 | case REG: | |
1510 | if (GET_MODE_BITSIZE (GET_MODE (x)) > BITS_PER_WORD) | |
1511 | { | |
1512 | if (LONG_REG_P (REGNO (x))) | |
1513 | fprintf (file, "(%s)", reg_names[REGNO (x)]); | |
1514 | else | |
1515 | fprintf (file, "(%s,%s)", reg_names[REGNO (x) + 1], | |
1516 | reg_names[REGNO (x)]); | |
1517 | } | |
1518 | else | |
1519 | fprintf (file, "%s", reg_names[REGNO (x)]); | |
1520 | return; | |
1521 | ||
1522 | case MEM: | |
1523 | output_address (XEXP (x, 0)); | |
1524 | return; | |
1525 | ||
1526 | case CONST_DOUBLE: | |
1527 | { | |
1528 | REAL_VALUE_TYPE r; | |
1529 | long l; | |
1530 | ||
1531 | REAL_VALUE_FROM_CONST_DOUBLE (r, x); | |
1532 | REAL_VALUE_TO_TARGET_SINGLE (r, l); | |
1533 | ||
1534 | fprintf (file, "$0x%lx", l); | |
1535 | return; | |
1536 | } | |
1537 | case CONST_INT: | |
1538 | { | |
1539 | fprintf (file, "$%ld", INTVAL (x)); | |
1540 | return; | |
1541 | } | |
1542 | case UNSPEC: | |
1543 | switch (XINT (x, 1)) | |
1544 | { | |
1545 | default: | |
1546 | gcc_unreachable (); | |
1547 | } | |
1548 | break; | |
1549 | ||
1550 | default: | |
1551 | if (!ptr_dereference) | |
1552 | { | |
1553 | putc ('$', file); | |
1554 | } | |
1555 | cr16_print_operand_address (file, x); | |
1556 | return; | |
1557 | } | |
1558 | default: | |
1559 | output_operand_lossage ("invalid %%xn code"); | |
1560 | } | |
1561 | ||
1562 | gcc_unreachable (); | |
1563 | } | |
1564 | ||
1565 | /* Implements the macro PRINT_OPERAND_ADDRESS defined in cr16.h. */ | |
1566 | ||
1567 | static void | |
1568 | cr16_print_operand_address (FILE * file, rtx addr) | |
1569 | { | |
1570 | enum cr16_addrtype addrtype; | |
1571 | struct cr16_address address; | |
1572 | ||
1573 | /* Decompose the address. Also ask it to treat address as constant. */ | |
1574 | addrtype = cr16_decompose_address (addr, &address, 0, TRUE); | |
1575 | ||
1576 | if (address.disp && GET_CODE (address.disp) == UNSPEC) | |
1577 | { | |
1578 | debug_rtx (addr); | |
1579 | } | |
1580 | ||
1581 | switch (addrtype) | |
1582 | { | |
1583 | case CR16_REG_REL: | |
1584 | if (address.disp) | |
1585 | { | |
1586 | if (GET_CODE (address.disp) == UNSPEC) | |
1587 | cr16_print_operand (file, address.disp, 0); | |
1588 | else | |
1589 | output_addr_const (file, address.disp); | |
1590 | } | |
1591 | else | |
1592 | fprintf (file, "0"); | |
1593 | fprintf (file, "(%s)", reg_names[REGNO (address.base)]); | |
1594 | break; | |
1595 | ||
1596 | case CR16_ABSOLUTE: | |
1597 | if (address.disp) | |
1598 | output_addr_const (file, address.disp); | |
1599 | else | |
1600 | fprintf (file, "0"); | |
1601 | break; | |
1602 | ||
1603 | case CR16_INDEX_REGP_REL: | |
1604 | fprintf (file, "[%s]", reg_names[REGNO (address.index)]); | |
1605 | /* Fall through. */ | |
1606 | case CR16_REGP_REL: | |
1607 | if (address.disp) | |
1608 | { | |
1609 | if (GET_CODE (address.disp) == UNSPEC) | |
1610 | cr16_print_operand (file, address.disp, 0); | |
1611 | else | |
1612 | output_addr_const (file, address.disp); | |
1613 | } | |
1614 | else | |
1615 | fprintf (file, "0"); | |
1616 | fprintf (file, "(%s,%s)", reg_names[REGNO (address.base) + 1], | |
1617 | reg_names[REGNO (address.base)]); | |
1618 | break; | |
1619 | default: | |
1620 | debug_rtx (addr); | |
1621 | gcc_unreachable (); | |
1622 | } | |
1623 | /* Add qualifiers to the address expression that was just printed. */ | |
1624 | if (flag_pic < NEAR_PIC && address.code == 0) | |
1625 | { | |
1626 | if (address.data == DM_FAR) | |
1627 | /* Addr contains SYMBOL_REF & far data ptr. */ | |
1628 | fprintf (file, "@l"); | |
1629 | else if (address.data == DM_DEFAULT) | |
1630 | /* Addr contains SYMBOL_REF & medium data ptr. */ | |
1631 | fprintf (file, "@m"); | |
1632 | /* Addr contains SYMBOL_REF & medium data ptr. */ | |
1633 | else if (address.data == DM_NEAR) | |
1634 | /* Addr contains SYMBOL_REF & near data ptr. */ | |
1635 | fprintf (file, "@s"); | |
1636 | } | |
1637 | else if (flag_pic == NEAR_PIC | |
1638 | && (address.code == 0) && (address.data == DM_FAR | |
1639 | || address.data == DM_DEFAULT | |
1640 | || address.data == DM_NEAR)) | |
1641 | { | |
1642 | fprintf (file, "@l"); | |
1643 | } | |
1644 | else if (flag_pic == NEAR_PIC && address.code == 2) | |
1645 | { | |
1646 | fprintf (file, "pic"); | |
1647 | } | |
1648 | else if (flag_pic == NEAR_PIC && address.code == 1) | |
1649 | { | |
1650 | fprintf (file, "@cpic"); | |
1651 | } | |
1652 | ||
1653 | else if (flag_pic == FAR_PIC && address.code == 2) | |
1654 | { | |
1655 | /* REVISIT: cr16 register indirect jump expects a 1-bit right shifted | |
1656 | address ! GOTc tells assembler this symbol is a text-address | |
1657 | This needs to be fixed in such a way that this offset is done | |
1658 | only in the case where an address is being used for indirect jump | |
1659 | or call. Determining the potential usage of loadd is of course not | |
1660 | possible always. Eventually, this has to be fixed in the | |
1661 | processor. */ | |
1662 | fprintf (file, "GOT (%s)", reg_names[PIC_OFFSET_TABLE_REGNUM]); | |
1663 | } | |
1664 | else if (flag_pic == FAR_PIC && address.code == 1) | |
1665 | { | |
1666 | fprintf (file, "@cGOT (%s)", reg_names[PIC_OFFSET_TABLE_REGNUM]); | |
1667 | } | |
1668 | ||
1669 | else if (flag_pic == FAR_PIC && | |
1670 | (address.data == DM_FAR || address.data == DM_DEFAULT | |
1671 | || address.data == DM_NEAR)) | |
1672 | { | |
1673 | fprintf (file, "@GOT (%s)", reg_names[PIC_OFFSET_TABLE_REGNUM]); | |
1674 | } | |
1675 | } | |
1676 | ||
1677 | /* Machine description helper functions. */ | |
1678 | ||
1679 | /* Called from cr16.md. The return value depends on the parameter push_or_pop: | |
1680 | When push_or_pop is zero -> string for push instructions of prologue. | |
1681 | When push_or_pop is nonzero -> string for pop/popret/retx in epilogue. | |
1682 | Relies on the assumptions: | |
1683 | 1. RA is the last register to be saved. | |
1684 | 2. The maximal value of the counter is MAX_COUNT. */ | |
1685 | char * | |
1686 | cr16_prepare_push_pop_string (int push_or_pop) | |
1687 | { | |
1688 | /* j is the number of registers being saved, takes care that there won't be | |
1689 | more than 8 in one push/pop instruction. */ | |
1690 | ||
1691 | /* For the register mask string. */ | |
1692 | static char one_inst_str[50]; | |
1693 | ||
1694 | /* i is the index of current_frame_info.save_regs[], going from 0 until | |
1695 | current_frame_info.last_reg_to_save. */ | |
1696 | int i, start_reg; | |
1697 | int word_cnt; | |
1698 | int print_ra; | |
1699 | char *return_str; | |
1700 | ||
1701 | /* For reversing on the push instructions if there are more than one. */ | |
1702 | char *temp_str; | |
1703 | ||
1704 | return_str = (char *) xmalloc (160); | |
1705 | temp_str = (char *) xmalloc (160); | |
1706 | ||
1707 | /* Initialize. */ | |
1708 | memset (return_str, 0, 3); | |
1709 | ||
1710 | i = 0; | |
1711 | while (i <= current_frame_info.last_reg_to_save) | |
1712 | { | |
1713 | /* Prepare mask for one instruction. */ | |
1714 | one_inst_str[0] = 0; | |
1715 | ||
1716 | /* To count number of words in one instruction. */ | |
1717 | word_cnt = 0; | |
1718 | start_reg = i; | |
1719 | print_ra = 0; | |
1720 | while ((word_cnt < MAX_COUNT) | |
1721 | && (i <= current_frame_info.last_reg_to_save)) | |
1722 | { | |
1723 | /* For each non consecutive save register, | |
1724 | a new instruction shall be generated. */ | |
1725 | if (!current_frame_info.save_regs[i]) | |
1726 | { | |
1727 | /* Move to next reg and break. */ | |
1728 | ++i; | |
1729 | break; | |
1730 | } | |
1731 | ||
1732 | if (i == RETURN_ADDRESS_REGNUM) | |
1733 | print_ra = 1; | |
1734 | else | |
1735 | { | |
1736 | /* Check especially if adding 2 does not cross the MAX_COUNT. */ | |
1737 | if ((word_cnt + ((i < CR16_FIRST_DWORD_REGISTER) ? 1 : 2)) | |
1738 | >= MAX_COUNT) | |
1739 | break; | |
1740 | /* Increase word count by 2 for long registers except RA. */ | |
1741 | word_cnt += ((i < CR16_FIRST_DWORD_REGISTER) ? 1 : 2); | |
1742 | } | |
1743 | ++i; | |
1744 | } | |
1745 | ||
1746 | /* No need to generate any instruction as | |
1747 | no register or RA needs to be saved. */ | |
1748 | if ((word_cnt == 0) && (print_ra == 0)) | |
1749 | continue; | |
1750 | ||
1751 | /* Now prepare the instruction operands. */ | |
1752 | if (word_cnt > 0) | |
1753 | { | |
1754 | sprintf (one_inst_str, "$%d, %s", word_cnt, reg_names[start_reg]); | |
1755 | if (print_ra) | |
1756 | strcat (one_inst_str, ", ra"); | |
1757 | } | |
1758 | else | |
1759 | strcat (one_inst_str, "ra"); | |
1760 | ||
1761 | if (push_or_pop == 1) | |
1762 | { | |
1763 | /* Pop instruction. */ | |
1764 | if (print_ra && !cr16_interrupt_function_p () | |
1765 | && !crtl->calls_eh_return) | |
1766 | /* Print popret if RA is saved and its not a interrupt | |
1767 | function. */ | |
1768 | strcpy (temp_str, "\n\tpopret\t"); | |
1769 | else | |
1770 | strcpy (temp_str, "\n\tpop\t"); | |
1771 | ||
1772 | strcat (temp_str, one_inst_str); | |
1773 | ||
1774 | /* Add the pop instruction list. */ | |
1775 | strcat (return_str, temp_str); | |
1776 | } | |
1777 | else | |
1778 | { | |
1779 | /* Push instruction. */ | |
1780 | strcpy (temp_str, "\n\tpush\t"); | |
1781 | strcat (temp_str, one_inst_str); | |
1782 | ||
1783 | /* We need to reverse the order of the instructions if there | |
1784 | are more than one. (since the pop will not be reversed in | |
1785 | the epilogue. */ | |
1786 | strcat (temp_str, return_str); | |
1787 | strcpy (return_str, temp_str); | |
1788 | } | |
1789 | } | |
1790 | ||
1791 | if (push_or_pop == 1) | |
1792 | { | |
1793 | /* POP. */ | |
1794 | if (cr16_interrupt_function_p ()) | |
1795 | strcat (return_str, "\n\tretx\n"); | |
1796 | else if (crtl->calls_eh_return) | |
1797 | { | |
1798 | /* Add stack adjustment before returning to exception handler | |
1799 | NOTE: EH_RETURN_STACKADJ_RTX must refer to (r5, r4). */ | |
1800 | strcat (return_str, "\n\taddd\t (r5, r4), (sp)\t\n"); | |
1801 | strcat (return_str, "\n\tjump\t (ra)\n"); | |
1802 | ||
1803 | /* But before anything else, undo the adjustment addition done in | |
1804 | cr16_expand_epilogue (). */ | |
1805 | strcpy (temp_str, "\n\tsubd\t (r5, r4), (sp)\t\n"); | |
1806 | strcat (temp_str, return_str); | |
1807 | strcpy (return_str, temp_str); | |
1808 | } | |
1809 | else if (!FUNC_IS_NORETURN_P (current_function_decl) | |
1810 | && !(current_frame_info.save_regs[RETURN_ADDRESS_REGNUM])) | |
1811 | strcat (return_str, "\n\tjump\t (ra)\n"); | |
1812 | } | |
1813 | ||
1814 | /* Skip the newline and the tab in the start of return_str. */ | |
1815 | return_str += 2; | |
1816 | return return_str; | |
1817 | } | |
1818 | ||
1819 | ||
1820 | /* Generate DWARF2 annotation for multi-push instruction. */ | |
1821 | static void | |
1822 | cr16_create_dwarf_for_multi_push (rtx insn) | |
1823 | { | |
1824 | rtx dwarf, reg, tmp; | |
1825 | int i, j, from, to, word_cnt, dwarf_par_index, inc; | |
3754d046 | 1826 | machine_mode mode; |
b9fdd12b | 1827 | int num_regs = 0, offset = 0, split_here = 0, total_push_bytes = 0; |
1828 | ||
1829 | for (i = 0; i <= current_frame_info.last_reg_to_save; ++i) | |
1830 | { | |
1831 | if (current_frame_info.save_regs[i]) | |
1832 | { | |
1833 | ++num_regs; | |
1834 | if (i < CR16_FIRST_DWORD_REGISTER) | |
1835 | total_push_bytes += 2; | |
1836 | else | |
1837 | total_push_bytes += 4; | |
1838 | } | |
1839 | } | |
1840 | ||
1841 | if (!num_regs) | |
1842 | return; | |
1843 | ||
1844 | dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_regs + 1)); | |
1845 | dwarf_par_index = num_regs; | |
1846 | ||
1847 | from = current_frame_info.last_reg_to_save + 1; | |
1848 | to = current_frame_info.last_reg_to_save; | |
1849 | word_cnt = 0; | |
1850 | ||
1851 | for (i = current_frame_info.last_reg_to_save; i >= 0;) | |
1852 | { | |
1853 | if (!current_frame_info.save_regs[i] || 0 == i || split_here) | |
1854 | { | |
1855 | /* This block of regs is pushed in one instruction. */ | |
1856 | if (0 == i && current_frame_info.save_regs[i]) | |
1857 | from = 0; | |
1858 | ||
1859 | for (j = to; j >= from; --j) | |
1860 | { | |
1861 | if (j < CR16_FIRST_DWORD_REGISTER) | |
1862 | { | |
1863 | mode = HImode; | |
1864 | inc = 1; | |
1865 | } | |
1866 | else | |
1867 | { | |
1868 | mode = SImode; | |
1869 | inc = 2; | |
1870 | } | |
1871 | reg = gen_rtx_REG (mode, j); | |
1872 | offset += 2 * inc; | |
d1f9b275 | 1873 | tmp = gen_rtx_SET (gen_frame_mem (mode, |
b9fdd12b | 1874 | plus_constant |
29c05e22 | 1875 | (Pmode, stack_pointer_rtx, |
b9fdd12b | 1876 | total_push_bytes - offset)), |
1877 | reg); | |
1878 | RTX_FRAME_RELATED_P (tmp) = 1; | |
1879 | XVECEXP (dwarf, 0, dwarf_par_index--) = tmp; | |
1880 | } | |
1881 | from = i; | |
1882 | to = --i; | |
1883 | split_here = 0; | |
1884 | word_cnt = 0; | |
1885 | continue; | |
1886 | } | |
1887 | ||
1888 | if (i != RETURN_ADDRESS_REGNUM) | |
1889 | { | |
1890 | inc = (i < CR16_FIRST_DWORD_REGISTER) ? 1 : 2; | |
1891 | if (word_cnt + inc >= MAX_COUNT || FRAME_POINTER_REGNUM == i) | |
1892 | { | |
1893 | split_here = 1; | |
1894 | from = i; | |
1895 | continue; | |
1896 | } | |
1897 | word_cnt += inc; | |
1898 | } | |
1899 | ||
1900 | from = i--; | |
1901 | } | |
1902 | ||
d1f9b275 | 1903 | tmp = gen_rtx_SET (stack_pointer_rtx, |
b9fdd12b | 1904 | gen_rtx_PLUS (SImode, stack_pointer_rtx, |
1905 | GEN_INT (-offset))); | |
1906 | RTX_FRAME_RELATED_P (tmp) = 1; | |
1907 | XVECEXP (dwarf, 0, 0) = tmp; | |
1908 | ||
1909 | add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf); | |
1910 | } | |
1911 | ||
1912 | /* | |
1913 | CompactRISC CR16 Architecture stack layout: | |
1914 | ||
1915 | 0 +--------------------- | |
1916 | | | |
1917 | . | |
1918 | . | |
1919 | | | |
1920 | +==================== Sp (x) = Ap (x+1) | |
1921 | A | Args for functions | |
1922 | | | called by X and Dynamically | |
1923 | | | Dynamic allocations allocated and | |
1924 | | | (alloca, variable deallocated | |
1925 | Stack | length arrays). | |
1926 | grows +-------------------- Fp (x) | |
1927 | down| | Local variables of X | |
1928 | ward| +-------------------- | |
1929 | | | Regs saved for X-1 | |
1930 | | +==================== Sp (x-1) = Ap (x) | |
1931 | | Args for func X | |
1932 | | pushed by X-1 | |
1933 | +-------------------- Fp (x-1) | |
1934 | | | |
1935 | | | |
1936 | V | |
1937 | */ | |
1938 | void | |
1939 | cr16_expand_prologue (void) | |
1940 | { | |
1941 | rtx insn; | |
1942 | ||
1943 | cr16_compute_frame (); | |
1944 | cr16_compute_save_regs (); | |
1945 | ||
1946 | /* If there is no need in push and adjustment to sp, return. */ | |
1947 | if ((current_frame_info.total_size + current_frame_info.reg_size) == 0) | |
1948 | return; | |
1949 | ||
1950 | if (current_frame_info.last_reg_to_save != -1) | |
1951 | { | |
1952 | /* If there are registers to push. */ | |
1953 | insn = emit_insn (gen_push_for_prologue | |
1954 | (GEN_INT (current_frame_info.reg_size))); | |
1955 | cr16_create_dwarf_for_multi_push (insn); | |
1956 | RTX_FRAME_RELATED_P (insn) = 1; | |
1957 | } | |
1958 | ||
1959 | ||
1960 | if (current_frame_info.total_size > 0) | |
1961 | { | |
1962 | insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, | |
1963 | GEN_INT (-current_frame_info.total_size))); | |
1964 | RTX_FRAME_RELATED_P (insn) = 1; | |
1965 | } | |
1966 | ||
1967 | if (frame_pointer_needed) | |
1968 | { | |
1969 | /* Initialize the frame pointer with the value of the stack pointer | |
1970 | pointing now to the locals. */ | |
1971 | insn = emit_move_insn (frame_pointer_rtx, stack_pointer_rtx); | |
1972 | } | |
1973 | } | |
1974 | ||
1975 | /* Generate insn that updates the stack for local variables and padding | |
1976 | for registers we save. - Generate the appropriate return insn. */ | |
1977 | void | |
1978 | cr16_expand_epilogue (void) | |
1979 | { | |
1980 | rtx insn; | |
1981 | ||
1982 | /* Nonzero if we need to return and pop only RA. This will generate a | |
1983 | different insn. This differentiate is for the peepholes for call as | |
1984 | last statement in function. */ | |
1985 | int only_popret_RA = (current_frame_info.save_regs[RETURN_ADDRESS_REGNUM] | |
1986 | && (current_frame_info.reg_size | |
1987 | == CR16_UNITS_PER_DWORD)); | |
1988 | ||
1989 | if (frame_pointer_needed) | |
1990 | { | |
1991 | /* Restore the stack pointer with the frame pointers value. */ | |
1992 | insn = emit_move_insn (stack_pointer_rtx, frame_pointer_rtx); | |
1993 | } | |
1994 | ||
1995 | if (current_frame_info.total_size > 0) | |
1996 | { | |
1997 | insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, | |
1998 | GEN_INT (current_frame_info.total_size))); | |
1999 | RTX_FRAME_RELATED_P (insn) = 1; | |
2000 | } | |
2001 | ||
2002 | if (crtl->calls_eh_return) | |
2003 | { | |
2004 | /* Add this here so that (r5, r4) is actually loaded with the adjustment | |
2005 | value; otherwise, the load might be optimized away... | |
2006 | NOTE: remember to subtract the adjustment before popping the regs | |
2007 | and add it back before returning. */ | |
2008 | insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, | |
2009 | EH_RETURN_STACKADJ_RTX)); | |
2010 | } | |
2011 | ||
2012 | if (cr16_interrupt_function_p ()) | |
2013 | { | |
2014 | insn = emit_jump_insn (gen_interrupt_return ()); | |
2015 | RTX_FRAME_RELATED_P (insn) = 1; | |
2016 | } | |
2017 | else if (crtl->calls_eh_return) | |
2018 | { | |
2019 | /* Special case, pop what's necessary, adjust SP and jump to (RA). */ | |
2020 | insn = emit_jump_insn (gen_pop_and_popret_return | |
2021 | (GEN_INT (current_frame_info.reg_size))); | |
2022 | RTX_FRAME_RELATED_P (insn) = 1; | |
2023 | } | |
2024 | else if (current_frame_info.last_reg_to_save == -1) | |
2025 | /* Nothing to pop. */ | |
2026 | /* Don't output jump for interrupt routine, only retx. */ | |
2027 | emit_jump_insn (gen_jump_return ()); | |
2028 | else if (only_popret_RA) | |
2029 | { | |
2030 | insn = emit_jump_insn (gen_popret_RA_return ()); | |
2031 | RTX_FRAME_RELATED_P (insn) = 1; | |
2032 | } | |
2033 | else | |
2034 | { | |
2035 | insn = emit_jump_insn (gen_pop_and_popret_return | |
2036 | (GEN_INT (current_frame_info.reg_size))); | |
2037 | RTX_FRAME_RELATED_P (insn) = 1; | |
2038 | } | |
2039 | } | |
2040 | ||
2041 | /* Implements FRAME_POINTER_REQUIRED. */ | |
2042 | static bool | |
2043 | cr16_frame_pointer_required (void) | |
2044 | { | |
2045 | return (cfun->calls_alloca || crtl->calls_eh_return | |
2046 | || cfun->has_nonlocal_label || crtl->calls_eh_return); | |
2047 | } | |
2048 | ||
2049 | static bool | |
2050 | cr16_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to) | |
2051 | { | |
2052 | return (to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true); | |
2053 | } | |
2054 | ||
2055 | ||
2056 | /* A C compound statement that attempts to replace X with | |
2057 | a valid memory address for an operand of mode MODE. WIN | |
2058 | will be a C statement label elsewhere in the code. | |
2059 | X will always be the result of a call to break_out_memory_refs (), | |
2060 | and OLDX will be the operand that was given to that function to | |
2061 | produce X. | |
2062 | The code generated by this macro should not alter the | |
2063 | substructure of X. If it transforms X into a more legitimate form, | |
2064 | it should assign X (which will always be a C variable) a new value. */ | |
2065 | static rtx | |
2066 | cr16_legitimize_address (rtx x, rtx orig_x ATTRIBUTE_UNUSED, | |
3754d046 | 2067 | machine_mode mode ATTRIBUTE_UNUSED) |
b9fdd12b | 2068 | { |
2069 | if (flag_pic) | |
2070 | return legitimize_pic_address (orig_x, mode, NULL_RTX); | |
2071 | else | |
2072 | return x; | |
2073 | } | |
2074 | ||
2075 | /* Implement TARGET_LEGITIMATE_CONSTANT_P | |
2076 | Nonzero if X is a legitimate constant for an immediate | |
2077 | operand on the target machine. You can assume that X | |
2078 | satisfies CONSTANT_P. In cr16c treat legitimize float | |
2079 | constant as an immediate operand. */ | |
2080 | static bool | |
3754d046 | 2081 | cr16_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, |
b9fdd12b | 2082 | rtx x ATTRIBUTE_UNUSED) |
2083 | { | |
2084 | return 1; | |
2085 | } | |
2086 | ||
2087 | void | |
2088 | notice_update_cc (rtx exp) | |
2089 | { | |
2090 | if (GET_CODE (exp) == SET) | |
2091 | { | |
2092 | /* Jumps do not alter the cc's. */ | |
2093 | if (SET_DEST (exp) == pc_rtx) | |
2094 | return; | |
2095 | ||
2096 | /* Moving register or memory into a register: | |
2097 | it doesn't alter the cc's, but it might invalidate | |
2098 | the RTX's which we remember the cc's came from. | |
2099 | (Note that moving a constant 0 or 1 MAY set the cc's). */ | |
2100 | if (REG_P (SET_DEST (exp)) | |
2101 | && (REG_P (SET_SRC (exp)) || GET_CODE (SET_SRC (exp)) == MEM)) | |
2102 | { | |
2103 | return; | |
2104 | } | |
2105 | ||
2106 | /* Moving register into memory doesn't alter the cc's. | |
2107 | It may invalidate the RTX's which we remember the cc's came from. */ | |
2108 | if (GET_CODE (SET_DEST (exp)) == MEM && REG_P (SET_SRC (exp))) | |
2109 | { | |
2110 | return; | |
2111 | } | |
2112 | } | |
2113 | ||
2114 | CC_STATUS_INIT; | |
2115 | return; | |
2116 | } | |
2117 | ||
3754d046 | 2118 | static machine_mode |
b9fdd12b | 2119 | cr16_unwind_word_mode (void) |
2120 | { | |
2121 | return SImode; | |
2122 | } | |
2123 | ||
2124 | /* Helper function for md file. This function is used to emit arithmetic | |
2125 | DI instructions. The argument "num" decides which instruction to be | |
2126 | printed. */ | |
2127 | const char * | |
2128 | cr16_emit_add_sub_di (rtx *operands, enum rtx_code code) | |
2129 | { | |
2130 | rtx lo_op[2] ; | |
2131 | rtx hi0_op[2] ; | |
2132 | rtx hi1_op[2] ; | |
2133 | ||
2134 | lo_op[0] = gen_lowpart (SImode, operands[0]); | |
2135 | hi0_op[0] = simplify_gen_subreg (HImode, operands[0], DImode, 4); | |
2136 | hi1_op[0] = simplify_gen_subreg (HImode, operands[0], DImode, 6); | |
2137 | ||
2138 | lo_op[1] = gen_lowpart (SImode, operands[2]); | |
2139 | hi0_op[1] = simplify_gen_subreg (HImode, operands[2], DImode, 4); | |
2140 | hi1_op[1] = simplify_gen_subreg (HImode, operands[2], DImode, 6); | |
2141 | ||
2142 | switch (code) | |
2143 | { | |
2144 | case PLUS: | |
2145 | { | |
2146 | output_asm_insn ("addd\t%1, %0", lo_op) ; | |
2147 | output_asm_insn ("addcw\t%1, %0", hi0_op) ; | |
2148 | output_asm_insn ("addcw\t%1, %0", hi1_op) ; | |
2149 | break; | |
2150 | } | |
2151 | case MINUS: | |
2152 | { | |
2153 | output_asm_insn ("subd\t%1, %0", lo_op) ; | |
2154 | output_asm_insn ("subcw\t%1, %0", hi0_op) ; | |
2155 | output_asm_insn ("subcw\t%1, %0", hi1_op) ; | |
2156 | break; | |
2157 | } | |
2158 | default: | |
2159 | break; | |
2160 | } | |
2161 | ||
2162 | return ""; | |
2163 | } | |
2164 | ||
2165 | ||
2166 | /* Helper function for md file. This function is used to emit logical | |
2167 | DI instructions. The argument "num" decides which instruction to be | |
2168 | printed. */ | |
2169 | const char * | |
2170 | cr16_emit_logical_di (rtx *operands, enum rtx_code code) | |
2171 | { | |
2172 | rtx lo_op[2] ; | |
2173 | rtx hi_op[2] ; | |
2174 | ||
2175 | lo_op[0] = gen_lowpart (SImode, operands[0]); | |
2176 | hi_op[0] = simplify_gen_subreg (SImode, operands[0], DImode, 4); | |
2177 | ||
2178 | lo_op[1] = gen_lowpart (SImode, operands[2]); | |
2179 | hi_op[1] = simplify_gen_subreg (SImode, operands[2], DImode, 4); | |
2180 | ||
2181 | switch (code) | |
2182 | { | |
2183 | case AND: | |
2184 | { | |
2185 | output_asm_insn ("andd\t%1, %0", lo_op) ; | |
2186 | output_asm_insn ("andd\t%1, %0", hi_op) ; | |
2187 | return ""; | |
2188 | } | |
2189 | case IOR: | |
2190 | { | |
2191 | output_asm_insn ("ord\t%1, %0", lo_op) ; | |
2192 | output_asm_insn ("ord\t%1, %0", hi_op) ; | |
2193 | return ""; | |
2194 | } | |
2195 | case XOR: | |
2196 | { | |
2197 | output_asm_insn ("xord\t%1, %0", lo_op) ; | |
2198 | output_asm_insn ("xord\t%1, %0", hi_op) ; | |
2199 | return ""; | |
2200 | } | |
2201 | default: | |
2202 | break; | |
2203 | } | |
2204 | ||
2205 | return ""; | |
2206 | } | |
2207 | ||
2208 | /* Initialize 'targetm' variable which contains pointers to functions | |
2209 | and data relating to the target machine. */ | |
2210 | ||
2211 | struct gcc_target targetm = TARGET_INITIALIZER; |