OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.2.2/] [gcc/] [config/] [arm/] [arm.c] - Blame information for rev 203

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 julius
/* Output routines for GCC for ARM.
2
   Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3
   2002, 2003, 2004, 2005, 2006, 2007  Free Software Foundation, Inc.
4
   Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5
   and Martin Simmons (@harleqn.co.uk).
6
   More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
 
8
   This file is part of GCC.
9
 
10
   GCC is free software; you can redistribute it and/or modify it
11
   under the terms of the GNU General Public License as published
12
   by the Free Software Foundation; either version 3, or (at your
13
   option) any later version.
14
 
15
   GCC is distributed in the hope that it will be useful, but WITHOUT
16
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18
   License for more details.
19
 
20
   You should have received a copy of the GNU General Public License
21
   along with GCC; see the file COPYING3.  If not see
22
   <http://www.gnu.org/licenses/>.  */
23
 
24
#include "config.h"
25
#include "system.h"
26
#include "coretypes.h"
27
#include "tm.h"
28
#include "rtl.h"
29
#include "tree.h"
30
#include "obstack.h"
31
#include "regs.h"
32
#include "hard-reg-set.h"
33
#include "real.h"
34
#include "insn-config.h"
35
#include "conditions.h"
36
#include "output.h"
37
#include "insn-attr.h"
38
#include "flags.h"
39
#include "reload.h"
40
#include "function.h"
41
#include "expr.h"
42
#include "optabs.h"
43
#include "toplev.h"
44
#include "recog.h"
45
#include "ggc.h"
46
#include "except.h"
47
#include "c-pragma.h"
48
#include "integrate.h"
49
#include "tm_p.h"
50
#include "target.h"
51
#include "target-def.h"
52
#include "debug.h"
53
#include "langhooks.h"
54
 
55
/* Forward definitions of types.  */
56
typedef struct minipool_node    Mnode;
57
typedef struct minipool_fixup   Mfix;
58
 
59
const struct attribute_spec arm_attribute_table[];
60
 
61
/* Forward function declarations.  */
62
static arm_stack_offsets *arm_get_frame_offsets (void);
63
static void arm_add_gc_roots (void);
64
static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
65
                             HOST_WIDE_INT, rtx, rtx, int, int);
66
static unsigned bit_count (unsigned long);
67
static int arm_address_register_rtx_p (rtx, int);
68
static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
69
static int thumb_base_register_rtx_p (rtx, enum machine_mode, int);
70
inline static int thumb_index_register_rtx_p (rtx, int);
71
static int thumb_far_jump_used_p (void);
72
static bool thumb_force_lr_save (void);
73
static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
74
static rtx emit_sfm (int, int);
75
static int arm_size_return_regs (void);
76
#ifndef AOF_ASSEMBLER
77
static bool arm_assemble_integer (rtx, unsigned int, int);
78
#endif
79
static const char *fp_const_from_val (REAL_VALUE_TYPE *);
80
static arm_cc get_arm_condition_code (rtx);
81
static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
82
static rtx is_jump_table (rtx);
83
static const char *output_multi_immediate (rtx *, const char *, const char *,
84
                                           int, HOST_WIDE_INT);
85
static const char *shift_op (rtx, HOST_WIDE_INT *);
86
static struct machine_function *arm_init_machine_status (void);
87
static void thumb_exit (FILE *, int);
88
static rtx is_jump_table (rtx);
89
static HOST_WIDE_INT get_jump_table_size (rtx);
90
static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
91
static Mnode *add_minipool_forward_ref (Mfix *);
92
static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
93
static Mnode *add_minipool_backward_ref (Mfix *);
94
static void assign_minipool_offsets (Mfix *);
95
static void arm_print_value (FILE *, rtx);
96
static void dump_minipool (rtx);
97
static int arm_barrier_cost (rtx);
98
static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
99
static void push_minipool_barrier (rtx, HOST_WIDE_INT);
100
static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
101
                               rtx);
102
static void arm_reorg (void);
103
static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
104
static int current_file_function_operand (rtx);
105
static unsigned long arm_compute_save_reg0_reg12_mask (void);
106
static unsigned long arm_compute_save_reg_mask (void);
107
static unsigned long arm_isr_value (tree);
108
static unsigned long arm_compute_func_type (void);
109
static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
110
static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
111
#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
112
static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
113
#endif
114
static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
115
static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
116
static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
117
static int arm_comp_type_attributes (tree, tree);
118
static void arm_set_default_type_attributes (tree);
119
static int arm_adjust_cost (rtx, rtx, rtx, int);
120
static int count_insns_for_constant (HOST_WIDE_INT, int);
121
static int arm_get_strip_length (int);
122
static bool arm_function_ok_for_sibcall (tree, tree);
123
static void arm_internal_label (FILE *, const char *, unsigned long);
124
static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
125
                                 tree);
126
static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
127
static bool arm_size_rtx_costs (rtx, int, int, int *);
128
static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
129
static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
130
static bool arm_xscale_rtx_costs (rtx, int, int, int *);
131
static bool arm_9e_rtx_costs (rtx, int, int, int *);
132
static int arm_address_cost (rtx);
133
static bool arm_memory_load_p (rtx);
134
static bool arm_cirrus_insn_p (rtx);
135
static void cirrus_reorg (rtx);
136
static void arm_init_builtins (void);
137
static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
138
static void arm_init_iwmmxt_builtins (void);
139
static rtx safe_vector_operand (rtx, enum machine_mode);
140
static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
141
static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
142
static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
143
static void emit_constant_insn (rtx cond, rtx pattern);
144
static rtx emit_set_insn (rtx, rtx);
145
static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
146
                                  tree, bool);
147
 
148
#ifdef OBJECT_FORMAT_ELF
149
static void arm_elf_asm_constructor (rtx, int);
150
#endif
151
#ifndef ARM_PE
152
static void arm_encode_section_info (tree, rtx, int);
153
#endif
154
 
155
static void arm_file_end (void);
156
 
157
#ifdef AOF_ASSEMBLER
158
static void aof_globalize_label (FILE *, const char *);
159
static void aof_dump_imports (FILE *);
160
static void aof_dump_pic_table (FILE *);
161
static void aof_file_start (void);
162
static void aof_file_end (void);
163
static void aof_asm_init_sections (void);
164
#endif
165
static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
166
                                        tree, int *, int);
167
static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
168
                                   enum machine_mode, tree, bool);
169
static bool arm_promote_prototypes (tree);
170
static bool arm_default_short_enums (void);
171
static bool arm_align_anon_bitfield (void);
172
static bool arm_return_in_msb (tree);
173
static bool arm_must_pass_in_stack (enum machine_mode, tree);
174
#ifdef TARGET_UNWIND_INFO
175
static void arm_unwind_emit (FILE *, rtx);
176
static bool arm_output_ttype (rtx);
177
#endif
178
 
179
static tree arm_cxx_guard_type (void);
180
static bool arm_cxx_guard_mask_bit (void);
181
static tree arm_get_cookie_size (tree);
182
static bool arm_cookie_has_size (void);
183
static bool arm_cxx_cdtor_returns_this (void);
184
static bool arm_cxx_key_method_may_be_inline (void);
185
static void arm_cxx_determine_class_data_visibility (tree);
186
static bool arm_cxx_class_data_always_comdat (void);
187
static bool arm_cxx_use_aeabi_atexit (void);
188
static void arm_init_libfuncs (void);
189
static bool arm_handle_option (size_t, const char *, int);
190
static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
191
static bool arm_cannot_copy_insn_p (rtx);
192
static bool arm_tls_symbol_p (rtx x);
193
 
194
 
195
/* Initialize the GCC target structure.  */
196
#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
197
#undef  TARGET_MERGE_DECL_ATTRIBUTES
198
#define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
199
#endif
200
 
201
#undef  TARGET_ATTRIBUTE_TABLE
202
#define TARGET_ATTRIBUTE_TABLE arm_attribute_table
203
 
204
#undef TARGET_ASM_FILE_END
205
#define TARGET_ASM_FILE_END arm_file_end
206
 
207
#ifdef AOF_ASSEMBLER
208
#undef  TARGET_ASM_BYTE_OP
209
#define TARGET_ASM_BYTE_OP "\tDCB\t"
210
#undef  TARGET_ASM_ALIGNED_HI_OP
211
#define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
212
#undef  TARGET_ASM_ALIGNED_SI_OP
213
#define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
214
#undef TARGET_ASM_GLOBALIZE_LABEL
215
#define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
216
#undef TARGET_ASM_FILE_START
217
#define TARGET_ASM_FILE_START aof_file_start
218
#undef TARGET_ASM_FILE_END
219
#define TARGET_ASM_FILE_END aof_file_end
220
#else
221
#undef  TARGET_ASM_ALIGNED_SI_OP
222
#define TARGET_ASM_ALIGNED_SI_OP NULL
223
#undef  TARGET_ASM_INTEGER
224
#define TARGET_ASM_INTEGER arm_assemble_integer
225
#endif
226
 
227
#undef  TARGET_ASM_FUNCTION_PROLOGUE
228
#define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
229
 
230
#undef  TARGET_ASM_FUNCTION_EPILOGUE
231
#define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
232
 
233
#undef  TARGET_DEFAULT_TARGET_FLAGS
234
#define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
235
#undef  TARGET_HANDLE_OPTION
236
#define TARGET_HANDLE_OPTION arm_handle_option
237
 
238
#undef  TARGET_COMP_TYPE_ATTRIBUTES
239
#define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
240
 
241
#undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
242
#define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
243
 
244
#undef  TARGET_SCHED_ADJUST_COST
245
#define TARGET_SCHED_ADJUST_COST arm_adjust_cost
246
 
247
#undef TARGET_ENCODE_SECTION_INFO
248
#ifdef ARM_PE
249
#define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
250
#else
251
#define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
252
#endif
253
 
254
#undef  TARGET_STRIP_NAME_ENCODING
255
#define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
256
 
257
#undef  TARGET_ASM_INTERNAL_LABEL
258
#define TARGET_ASM_INTERNAL_LABEL arm_internal_label
259
 
260
#undef  TARGET_FUNCTION_OK_FOR_SIBCALL
261
#define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
262
 
263
#undef  TARGET_ASM_OUTPUT_MI_THUNK
264
#define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
265
#undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
266
#define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
267
 
268
/* This will be overridden in arm_override_options.  */
269
#undef  TARGET_RTX_COSTS
270
#define TARGET_RTX_COSTS arm_slowmul_rtx_costs
271
#undef  TARGET_ADDRESS_COST
272
#define TARGET_ADDRESS_COST arm_address_cost
273
 
274
#undef TARGET_SHIFT_TRUNCATION_MASK
275
#define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
276
#undef TARGET_VECTOR_MODE_SUPPORTED_P
277
#define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
278
 
279
#undef  TARGET_MACHINE_DEPENDENT_REORG
280
#define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
281
 
282
#undef  TARGET_INIT_BUILTINS
283
#define TARGET_INIT_BUILTINS  arm_init_builtins
284
#undef  TARGET_EXPAND_BUILTIN
285
#define TARGET_EXPAND_BUILTIN arm_expand_builtin
286
 
287
#undef TARGET_INIT_LIBFUNCS
288
#define TARGET_INIT_LIBFUNCS arm_init_libfuncs
289
 
290
#undef TARGET_PROMOTE_FUNCTION_ARGS
291
#define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
292
#undef TARGET_PROMOTE_FUNCTION_RETURN
293
#define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
294
#undef TARGET_PROMOTE_PROTOTYPES
295
#define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
296
#undef TARGET_PASS_BY_REFERENCE
297
#define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
298
#undef TARGET_ARG_PARTIAL_BYTES
299
#define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
300
 
301
#undef  TARGET_SETUP_INCOMING_VARARGS
302
#define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
303
 
304
#undef TARGET_DEFAULT_SHORT_ENUMS
305
#define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
306
 
307
#undef TARGET_ALIGN_ANON_BITFIELD
308
#define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
309
 
310
#undef TARGET_NARROW_VOLATILE_BITFIELD
311
#define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
312
 
313
#undef TARGET_CXX_GUARD_TYPE
314
#define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
315
 
316
#undef TARGET_CXX_GUARD_MASK_BIT
317
#define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
318
 
319
#undef TARGET_CXX_GET_COOKIE_SIZE
320
#define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
321
 
322
#undef TARGET_CXX_COOKIE_HAS_SIZE
323
#define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
324
 
325
#undef TARGET_CXX_CDTOR_RETURNS_THIS
326
#define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
327
 
328
#undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
329
#define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
330
 
331
#undef TARGET_CXX_USE_AEABI_ATEXIT
332
#define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
333
 
334
#undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
335
#define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
336
  arm_cxx_determine_class_data_visibility
337
 
338
#undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
339
#define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
340
 
341
#undef TARGET_RETURN_IN_MSB
342
#define TARGET_RETURN_IN_MSB arm_return_in_msb
343
 
344
#undef TARGET_MUST_PASS_IN_STACK
345
#define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
346
 
347
#ifdef TARGET_UNWIND_INFO
348
#undef TARGET_UNWIND_EMIT
349
#define TARGET_UNWIND_EMIT arm_unwind_emit
350
 
351
/* EABI unwinding tables use a different format for the typeinfo tables.  */
352
#undef TARGET_ASM_TTYPE
353
#define TARGET_ASM_TTYPE arm_output_ttype
354
 
355
#undef TARGET_ARM_EABI_UNWINDER
356
#define TARGET_ARM_EABI_UNWINDER true
357
#endif /* TARGET_UNWIND_INFO */
358
 
359
#undef  TARGET_CANNOT_COPY_INSN_P
360
#define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
361
 
362
#ifdef HAVE_AS_TLS
363
#undef TARGET_HAVE_TLS
364
#define TARGET_HAVE_TLS true
365
#endif
366
 
367
#undef TARGET_CANNOT_FORCE_CONST_MEM
368
#define TARGET_CANNOT_FORCE_CONST_MEM arm_tls_referenced_p
369
 
370
struct gcc_target targetm = TARGET_INITIALIZER;
371
 
372
/* Obstack for minipool constant handling.  */
373
static struct obstack minipool_obstack;
374
static char *         minipool_startobj;
375
 
376
/* The maximum number of insns skipped which
377
   will be conditionalised if possible.  */
378
static int max_insns_skipped = 5;
379
 
380
extern FILE * asm_out_file;
381
 
382
/* True if we are currently building a constant table.  */
383
int making_const_table;
384
 
385
/* Define the information needed to generate branch insns.  This is
386
   stored from the compare operation.  */
387
rtx arm_compare_op0, arm_compare_op1;
388
 
389
/* The processor for which instructions should be scheduled.  */
390
enum processor_type arm_tune = arm_none;
391
 
392
/* Which floating point model to use.  */
393
enum arm_fp_model arm_fp_model;
394
 
395
/* Which floating point hardware is available.  */
396
enum fputype arm_fpu_arch;
397
 
398
/* Which floating point hardware to schedule for.  */
399
enum fputype arm_fpu_tune;
400
 
401
/* Whether to use floating point hardware.  */
402
enum float_abi_type arm_float_abi;
403
 
404
/* Which ABI to use.  */
405
enum arm_abi_type arm_abi;
406
 
407
/* Which thread pointer model to use.  */
408
enum arm_tp_type target_thread_pointer = TP_AUTO;
409
 
410
/* Used to parse -mstructure_size_boundary command line option.  */
411
int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
412
 
413
/* Used for Thumb call_via trampolines.  */
414
rtx thumb_call_via_label[14];
415
static int thumb_call_reg_needed;
416
 
417
/* Bit values used to identify processor capabilities.  */
418
#define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
419
#define FL_ARCH3M     (1 << 1)        /* Extended multiply */
420
#define FL_MODE26     (1 << 2)        /* 26-bit mode support */
421
#define FL_MODE32     (1 << 3)        /* 32-bit mode support */
422
#define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
423
#define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
424
#define FL_THUMB      (1 << 6)        /* Thumb aware */
425
#define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
426
#define FL_STRONG     (1 << 8)        /* StrongARM */
427
#define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
428
#define FL_XSCALE     (1 << 10)       /* XScale */
429
#define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
430
#define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
431
                                         media instructions.  */
432
#define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
433
#define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
434
                                         Note: ARM6 & 7 derivatives only.  */
435
#define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
436
 
437
#define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
438
 
439
#define FL_FOR_ARCH2    0
440
#define FL_FOR_ARCH3    FL_MODE32
441
#define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
442
#define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
443
#define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
444
#define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
445
#define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
446
#define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
447
#define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
448
#define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
449
#define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
450
#define FL_FOR_ARCH6J   FL_FOR_ARCH6
451
#define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
452
#define FL_FOR_ARCH6Z   FL_FOR_ARCH6
453
#define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
454
 
455
/* The bits in this mask specify which
456
   instructions we are allowed to generate.  */
457
static unsigned long insn_flags = 0;
458
 
459
/* The bits in this mask specify which instruction scheduling options should
460
   be used.  */
461
static unsigned long tune_flags = 0;
462
 
463
/* The following are used in the arm.md file as equivalents to bits
464
   in the above two flag variables.  */
465
 
466
/* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
467
int arm_arch3m = 0;
468
 
469
/* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
470
int arm_arch4 = 0;
471
 
472
/* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
473
int arm_arch4t = 0;
474
 
475
/* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
476
int arm_arch5 = 0;
477
 
478
/* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
479
int arm_arch5e = 0;
480
 
481
/* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
482
int arm_arch6 = 0;
483
 
484
/* Nonzero if this chip supports the ARM 6K extensions.  */
485
int arm_arch6k = 0;
486
 
487
/* Nonzero if this chip can benefit from load scheduling.  */
488
int arm_ld_sched = 0;
489
 
490
/* Nonzero if this chip is a StrongARM.  */
491
int arm_tune_strongarm = 0;
492
 
493
/* Nonzero if this chip is a Cirrus variant.  */
494
int arm_arch_cirrus = 0;
495
 
496
/* Nonzero if this chip supports Intel Wireless MMX technology.  */
497
int arm_arch_iwmmxt = 0;
498
 
499
/* Nonzero if this chip is an XScale.  */
500
int arm_arch_xscale = 0;
501
 
502
/* Nonzero if tuning for XScale  */
503
int arm_tune_xscale = 0;
504
 
505
/* Nonzero if we want to tune for stores that access the write-buffer.
506
   This typically means an ARM6 or ARM7 with MMU or MPU.  */
507
int arm_tune_wbuf = 0;
508
 
509
/* Nonzero if generating Thumb instructions.  */
510
int thumb_code = 0;
511
 
512
/* Nonzero if we should define __THUMB_INTERWORK__ in the
513
   preprocessor.
514
   XXX This is a bit of a hack, it's intended to help work around
515
   problems in GLD which doesn't understand that armv5t code is
516
   interworking clean.  */
517
int arm_cpp_interwork = 0;
518
 
519
/* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
520
   must report the mode of the memory reference from PRINT_OPERAND to
521
   PRINT_OPERAND_ADDRESS.  */
522
enum machine_mode output_memory_reference_mode;
523
 
524
/* The register number to be used for the PIC offset register.  */
525
unsigned arm_pic_register = INVALID_REGNUM;
526
 
527
/* Set to 1 when a return insn is output, this means that the epilogue
528
   is not needed.  */
529
int return_used_this_function;
530
 
531
/* Set to 1 after arm_reorg has started.  Reset to start at the start of
532
   the next function.  */
533
static int after_arm_reorg = 0;
534
 
535
/* The maximum number of insns to be used when loading a constant.  */
536
static int arm_constant_limit = 3;
537
 
538
/* For an explanation of these variables, see final_prescan_insn below.  */
539
int arm_ccfsm_state;
540
enum arm_cond_code arm_current_cc;
541
rtx arm_target_insn;
542
int arm_target_label;
543
 
544
/* The condition codes of the ARM, and the inverse function.  */
545
static const char * const arm_condition_codes[] =
546
{
547
  "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
548
  "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
549
};
550
 
551
#define streq(string1, string2) (strcmp (string1, string2) == 0)
552
 
553
/* Initialization code.  */
554
 
555
struct processors
556
{
557
  const char *const name;
558
  enum processor_type core;
559
  const char *arch;
560
  const unsigned long flags;
561
  bool (* rtx_costs) (rtx, int, int, int *);
562
};
563
 
564
/* Not all of these give usefully different compilation alternatives,
565
   but there is no simple way of generalizing them.  */
566
static const struct processors all_cores[] =
567
{
568
  /* ARM Cores */
569
#define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
570
  {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
571
#include "arm-cores.def"
572
#undef ARM_CORE
573
  {NULL, arm_none, NULL, 0, NULL}
574
};
575
 
576
static const struct processors all_architectures[] =
577
{
578
  /* ARM Architectures */
579
  /* We don't specify rtx_costs here as it will be figured out
580
     from the core.  */
581
 
582
  {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
583
  {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
584
  {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
585
  {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
586
  {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
587
  /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
588
     implementations that support it, so we will leave it out for now.  */
589
  {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
590
  {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
591
  {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
592
  {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
593
  {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
594
  {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
595
  {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
596
  {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
597
  {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
598
  {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
599
  {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
600
  {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
601
  {NULL, arm_none, NULL, 0 , NULL}
602
};
603
 
604
struct arm_cpu_select
605
{
606
  const char *              string;
607
  const char *              name;
608
  const struct processors * processors;
609
};
610
 
611
/* This is a magic structure.  The 'string' field is magically filled in
612
   with a pointer to the value specified by the user on the command line
613
   assuming that the user has specified such a value.  */
614
 
615
static struct arm_cpu_select arm_select[] =
616
{
617
  /* string       name            processors  */
618
  { NULL,       "-mcpu=",       all_cores  },
619
  { NULL,       "-march=",      all_architectures },
620
  { NULL,       "-mtune=",      all_cores }
621
};
622
 
623
/* Defines representing the indexes into the above table.  */
624
#define ARM_OPT_SET_CPU 0
625
#define ARM_OPT_SET_ARCH 1
626
#define ARM_OPT_SET_TUNE 2
627
 
628
/* The name of the preprocessor macro to define for this architecture.  */
629
 
630
char arm_arch_name[] = "__ARM_ARCH_0UNK__";
631
 
632
struct fpu_desc
633
{
634
  const char * name;
635
  enum fputype fpu;
636
};
637
 
638
 
639
/* Available values for -mfpu=.  */
640
 
641
static const struct fpu_desc all_fpus[] =
642
{
643
  {"fpa",       FPUTYPE_FPA},
644
  {"fpe2",      FPUTYPE_FPA_EMU2},
645
  {"fpe3",      FPUTYPE_FPA_EMU2},
646
  {"maverick",  FPUTYPE_MAVERICK},
647
  {"vfp",       FPUTYPE_VFP}
648
};
649
 
650
 
651
/* Floating point models used by the different hardware.
652
   See fputype in arm.h.  */
653
 
654
static const enum fputype fp_model_for_fpu[] =
655
{
656
  /* No FP hardware.  */
657
  ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
658
  ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
659
  ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
660
  ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
661
  ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
662
  ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
663
};
664
 
665
 
666
struct float_abi
667
{
668
  const char * name;
669
  enum float_abi_type abi_type;
670
};
671
 
672
 
673
/* Available values for -mfloat-abi=.  */
674
 
675
static const struct float_abi all_float_abis[] =
676
{
677
  {"soft",      ARM_FLOAT_ABI_SOFT},
678
  {"softfp",    ARM_FLOAT_ABI_SOFTFP},
679
  {"hard",      ARM_FLOAT_ABI_HARD}
680
};
681
 
682
 
683
struct abi_name
684
{
685
  const char *name;
686
  enum arm_abi_type abi_type;
687
};
688
 
689
 
690
/* Available values for -mabi=.  */
691
 
692
static const struct abi_name arm_all_abis[] =
693
{
694
  {"apcs-gnu",    ARM_ABI_APCS},
695
  {"atpcs",   ARM_ABI_ATPCS},
696
  {"aapcs",   ARM_ABI_AAPCS},
697
  {"iwmmxt",  ARM_ABI_IWMMXT},
698
  {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
699
};
700
 
701
/* Supported TLS relocations.  */
702
 
703
enum tls_reloc {
704
  TLS_GD32,
705
  TLS_LDM32,
706
  TLS_LDO32,
707
  TLS_IE32,
708
  TLS_LE32
709
};
710
 
711
/* Emit an insn that's a simple single-set.  Both the operands must be known
712
   to be valid.  */
713
inline static rtx
714
emit_set_insn (rtx x, rtx y)
715
{
716
  return emit_insn (gen_rtx_SET (VOIDmode, x, y));
717
}
718
 
719
/* Return the number of bits set in VALUE.  */
720
static unsigned
721
bit_count (unsigned long value)
722
{
723
  unsigned long count = 0;
724
 
725
  while (value)
726
    {
727
      count++;
728
      value &= value - 1;  /* Clear the least-significant set bit.  */
729
    }
730
 
731
  return count;
732
}
733
 
734
/* Set up library functions unique to ARM.  */
735
 
736
static void
737
arm_init_libfuncs (void)
738
{
739
  /* There are no special library functions unless we are using the
740
     ARM BPABI.  */
741
  if (!TARGET_BPABI)
742
    return;
743
 
744
  /* The functions below are described in Section 4 of the "Run-Time
745
     ABI for the ARM architecture", Version 1.0.  */
746
 
747
  /* Double-precision floating-point arithmetic.  Table 2.  */
748
  set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
749
  set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
750
  set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
751
  set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
752
  set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
753
 
754
  /* Double-precision comparisons.  Table 3.  */
755
  set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
756
  set_optab_libfunc (ne_optab, DFmode, NULL);
757
  set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
758
  set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
759
  set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
760
  set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
761
  set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
762
 
763
  /* Single-precision floating-point arithmetic.  Table 4.  */
764
  set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
765
  set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
766
  set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
767
  set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
768
  set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
769
 
770
  /* Single-precision comparisons.  Table 5.  */
771
  set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
772
  set_optab_libfunc (ne_optab, SFmode, NULL);
773
  set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
774
  set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
775
  set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
776
  set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
777
  set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
778
 
779
  /* Floating-point to integer conversions.  Table 6.  */
780
  set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
781
  set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
782
  set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
783
  set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
784
  set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
785
  set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
786
  set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
787
  set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
788
 
789
  /* Conversions between floating types.  Table 7.  */
790
  set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
791
  set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
792
 
793
  /* Integer to floating-point conversions.  Table 8.  */
794
  set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
795
  set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
796
  set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
797
  set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
798
  set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
799
  set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
800
  set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
801
  set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
802
 
803
  /* Long long.  Table 9.  */
804
  set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
805
  set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
806
  set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
807
  set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
808
  set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
809
  set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
810
  set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
811
  set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
812
 
813
  /* Integer (32/32->32) division.  \S 4.3.1.  */
814
  set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
815
  set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
816
 
817
  /* The divmod functions are designed so that they can be used for
818
     plain division, even though they return both the quotient and the
819
     remainder.  The quotient is returned in the usual location (i.e.,
820
     r0 for SImode, {r0, r1} for DImode), just as would be expected
821
     for an ordinary division routine.  Because the AAPCS calling
822
     conventions specify that all of { r0, r1, r2, r3 } are
823
     callee-saved registers, there is no need to tell the compiler
824
     explicitly that those registers are clobbered by these
825
     routines.  */
826
  set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
827
  set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
828
 
829
  /* For SImode division the ABI provides div-without-mod routines,
830
     which are faster.  */
831
  set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
832
  set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
833
 
834
  /* We don't have mod libcalls.  Fortunately gcc knows how to use the
835
     divmod libcalls instead.  */
836
  set_optab_libfunc (smod_optab, DImode, NULL);
837
  set_optab_libfunc (umod_optab, DImode, NULL);
838
  set_optab_libfunc (smod_optab, SImode, NULL);
839
  set_optab_libfunc (umod_optab, SImode, NULL);
840
}
841
 
842
/* Implement TARGET_HANDLE_OPTION.  */
843
 
844
static bool
845
arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
846
{
847
  switch (code)
848
    {
849
    case OPT_march_:
850
      arm_select[1].string = arg;
851
      return true;
852
 
853
    case OPT_mcpu_:
854
      arm_select[0].string = arg;
855
      return true;
856
 
857
    case OPT_mhard_float:
858
      target_float_abi_name = "hard";
859
      return true;
860
 
861
    case OPT_msoft_float:
862
      target_float_abi_name = "soft";
863
      return true;
864
 
865
    case OPT_mtune_:
866
      arm_select[2].string = arg;
867
      return true;
868
 
869
    default:
870
      return true;
871
    }
872
}
873
 
874
/* Fix up any incompatible options that the user has specified.
875
   This has now turned into a maze.  */
876
void
877
arm_override_options (void)
878
{
879
  unsigned i;
880
  enum processor_type target_arch_cpu = arm_none;
881
 
882
  /* Set up the flags based on the cpu/architecture selected by the user.  */
883
  for (i = ARRAY_SIZE (arm_select); i--;)
884
    {
885
      struct arm_cpu_select * ptr = arm_select + i;
886
 
887
      if (ptr->string != NULL && ptr->string[0] != '\0')
888
        {
889
          const struct processors * sel;
890
 
891
          for (sel = ptr->processors; sel->name != NULL; sel++)
892
            if (streq (ptr->string, sel->name))
893
              {
894
                /* Set the architecture define.  */
895
                if (i != ARM_OPT_SET_TUNE)
896
                  sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
897
 
898
                /* Determine the processor core for which we should
899
                   tune code-generation.  */
900
                if (/* -mcpu= is a sensible default.  */
901
                    i == ARM_OPT_SET_CPU
902
                    /* -mtune= overrides -mcpu= and -march=.  */
903
                    || i == ARM_OPT_SET_TUNE)
904
                  arm_tune = (enum processor_type) (sel - ptr->processors);
905
 
906
                /* Remember the CPU associated with this architecture.
907
                   If no other option is used to set the CPU type,
908
                   we'll use this to guess the most suitable tuning
909
                   options.  */
910
                if (i == ARM_OPT_SET_ARCH)
911
                  target_arch_cpu = sel->core;
912
 
913
                if (i != ARM_OPT_SET_TUNE)
914
                  {
915
                    /* If we have been given an architecture and a processor
916
                       make sure that they are compatible.  We only generate
917
                       a warning though, and we prefer the CPU over the
918
                       architecture.  */
919
                    if (insn_flags != 0 && (insn_flags ^ sel->flags))
920
                      warning (0, "switch -mcpu=%s conflicts with -march= switch",
921
                               ptr->string);
922
 
923
                    insn_flags = sel->flags;
924
                  }
925
 
926
                break;
927
              }
928
 
929
          if (sel->name == NULL)
930
            error ("bad value (%s) for %s switch", ptr->string, ptr->name);
931
        }
932
    }
933
 
934
  /* Guess the tuning options from the architecture if necessary.  */
935
  if (arm_tune == arm_none)
936
    arm_tune = target_arch_cpu;
937
 
938
  /* If the user did not specify a processor, choose one for them.  */
939
  if (insn_flags == 0)
940
    {
941
      const struct processors * sel;
942
      unsigned int        sought;
943
      enum processor_type cpu;
944
 
945
      cpu = TARGET_CPU_DEFAULT;
946
      if (cpu == arm_none)
947
        {
948
#ifdef SUBTARGET_CPU_DEFAULT
949
          /* Use the subtarget default CPU if none was specified by
950
             configure.  */
951
          cpu = SUBTARGET_CPU_DEFAULT;
952
#endif
953
          /* Default to ARM6.  */
954
          if (cpu == arm_none)
955
            cpu = arm6;
956
        }
957
      sel = &all_cores[cpu];
958
 
959
      insn_flags = sel->flags;
960
 
961
      /* Now check to see if the user has specified some command line
962
         switch that require certain abilities from the cpu.  */
963
      sought = 0;
964
 
965
      if (TARGET_INTERWORK || TARGET_THUMB)
966
        {
967
          sought |= (FL_THUMB | FL_MODE32);
968
 
969
          /* There are no ARM processors that support both APCS-26 and
970
             interworking.  Therefore we force FL_MODE26 to be removed
971
             from insn_flags here (if it was set), so that the search
972
             below will always be able to find a compatible processor.  */
973
          insn_flags &= ~FL_MODE26;
974
        }
975
 
976
      if (sought != 0 && ((sought & insn_flags) != sought))
977
        {
978
          /* Try to locate a CPU type that supports all of the abilities
979
             of the default CPU, plus the extra abilities requested by
980
             the user.  */
981
          for (sel = all_cores; sel->name != NULL; sel++)
982
            if ((sel->flags & sought) == (sought | insn_flags))
983
              break;
984
 
985
          if (sel->name == NULL)
986
            {
987
              unsigned current_bit_count = 0;
988
              const struct processors * best_fit = NULL;
989
 
990
              /* Ideally we would like to issue an error message here
991
                 saying that it was not possible to find a CPU compatible
992
                 with the default CPU, but which also supports the command
993
                 line options specified by the programmer, and so they
994
                 ought to use the -mcpu=<name> command line option to
995
                 override the default CPU type.
996
 
997
                 If we cannot find a cpu that has both the
998
                 characteristics of the default cpu and the given
999
                 command line options we scan the array again looking
1000
                 for a best match.  */
1001
              for (sel = all_cores; sel->name != NULL; sel++)
1002
                if ((sel->flags & sought) == sought)
1003
                  {
1004
                    unsigned count;
1005
 
1006
                    count = bit_count (sel->flags & insn_flags);
1007
 
1008
                    if (count >= current_bit_count)
1009
                      {
1010
                        best_fit = sel;
1011
                        current_bit_count = count;
1012
                      }
1013
                  }
1014
 
1015
              gcc_assert (best_fit);
1016
              sel = best_fit;
1017
            }
1018
 
1019
          insn_flags = sel->flags;
1020
        }
1021
      sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1022
      if (arm_tune == arm_none)
1023
        arm_tune = (enum processor_type) (sel - all_cores);
1024
    }
1025
 
1026
  /* The processor for which we should tune should now have been
1027
     chosen.  */
1028
  gcc_assert (arm_tune != arm_none);
1029
 
1030
  tune_flags = all_cores[(int)arm_tune].flags;
1031
  if (optimize_size)
1032
    targetm.rtx_costs = arm_size_rtx_costs;
1033
  else
1034
    targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1035
 
1036
  /* Make sure that the processor choice does not conflict with any of the
1037
     other command line choices.  */
1038
  if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1039
    {
1040
      warning (0, "target CPU does not support interworking" );
1041
      target_flags &= ~MASK_INTERWORK;
1042
    }
1043
 
1044
  if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1045
    {
1046
      warning (0, "target CPU does not support THUMB instructions");
1047
      target_flags &= ~MASK_THUMB;
1048
    }
1049
 
1050
  if (TARGET_APCS_FRAME && TARGET_THUMB)
1051
    {
1052
      /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1053
      target_flags &= ~MASK_APCS_FRAME;
1054
    }
1055
 
1056
  /* Callee super interworking implies thumb interworking.  Adding
1057
     this to the flags here simplifies the logic elsewhere.  */
1058
  if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1059
      target_flags |= MASK_INTERWORK;
1060
 
1061
  /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1062
     from here where no function is being compiled currently.  */
1063
  if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1064
    warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1065
 
1066
  if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1067
    warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1068
 
1069
  if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1070
    warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1071
 
1072
  if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1073
    {
1074
      warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1075
      target_flags |= MASK_APCS_FRAME;
1076
    }
1077
 
1078
  if (TARGET_POKE_FUNCTION_NAME)
1079
    target_flags |= MASK_APCS_FRAME;
1080
 
1081
  if (TARGET_APCS_REENT && flag_pic)
1082
    error ("-fpic and -mapcs-reent are incompatible");
1083
 
1084
  if (TARGET_APCS_REENT)
1085
    warning (0, "APCS reentrant code not supported.  Ignored");
1086
 
1087
  /* If this target is normally configured to use APCS frames, warn if they
1088
     are turned off and debugging is turned on.  */
1089
  if (TARGET_ARM
1090
      && write_symbols != NO_DEBUG
1091
      && !TARGET_APCS_FRAME
1092
      && (TARGET_DEFAULT & MASK_APCS_FRAME))
1093
    warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1094
 
1095
  /* If stack checking is disabled, we can use r10 as the PIC register,
1096
     which keeps r9 available.  */
1097
  if (flag_pic && TARGET_SINGLE_PIC_BASE)
1098
    arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1099
 
1100
  if (TARGET_APCS_FLOAT)
1101
    warning (0, "passing floating point arguments in fp regs not yet supported");
1102
 
1103
  /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1104
  arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1105
  arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1106
  arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1107
  arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1108
  arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1109
  arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1110
  arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1111
  arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1112
  arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1113
 
1114
  arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1115
  arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1116
  thumb_code = (TARGET_ARM == 0);
1117
  arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1118
  arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1119
  arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1120
 
1121
  /* V5 code we generate is completely interworking capable, so we turn off
1122
     TARGET_INTERWORK here to avoid many tests later on.  */
1123
 
1124
  /* XXX However, we must pass the right pre-processor defines to CPP
1125
     or GLD can get confused.  This is a hack.  */
1126
  if (TARGET_INTERWORK)
1127
    arm_cpp_interwork = 1;
1128
 
1129
  if (arm_arch5)
1130
    target_flags &= ~MASK_INTERWORK;
1131
 
1132
  if (target_abi_name)
1133
    {
1134
      for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1135
        {
1136
          if (streq (arm_all_abis[i].name, target_abi_name))
1137
            {
1138
              arm_abi = arm_all_abis[i].abi_type;
1139
              break;
1140
            }
1141
        }
1142
      if (i == ARRAY_SIZE (arm_all_abis))
1143
        error ("invalid ABI option: -mabi=%s", target_abi_name);
1144
    }
1145
  else
1146
    arm_abi = ARM_DEFAULT_ABI;
1147
 
1148
  if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1149
    error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1150
 
1151
  if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1152
    error ("iwmmxt abi requires an iwmmxt capable cpu");
1153
 
1154
  arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1155
  if (target_fpu_name == NULL && target_fpe_name != NULL)
1156
    {
1157
      if (streq (target_fpe_name, "2"))
1158
        target_fpu_name = "fpe2";
1159
      else if (streq (target_fpe_name, "3"))
1160
        target_fpu_name = "fpe3";
1161
      else
1162
        error ("invalid floating point emulation option: -mfpe=%s",
1163
               target_fpe_name);
1164
    }
1165
  if (target_fpu_name != NULL)
1166
    {
1167
      /* The user specified a FPU.  */
1168
      for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1169
        {
1170
          if (streq (all_fpus[i].name, target_fpu_name))
1171
            {
1172
              arm_fpu_arch = all_fpus[i].fpu;
1173
              arm_fpu_tune = arm_fpu_arch;
1174
              arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1175
              break;
1176
            }
1177
        }
1178
      if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1179
        error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1180
    }
1181
  else
1182
    {
1183
#ifdef FPUTYPE_DEFAULT
1184
      /* Use the default if it is specified for this platform.  */
1185
      arm_fpu_arch = FPUTYPE_DEFAULT;
1186
      arm_fpu_tune = FPUTYPE_DEFAULT;
1187
#else
1188
      /* Pick one based on CPU type.  */
1189
      /* ??? Some targets assume FPA is the default.
1190
      if ((insn_flags & FL_VFP) != 0)
1191
        arm_fpu_arch = FPUTYPE_VFP;
1192
      else
1193
      */
1194
      if (arm_arch_cirrus)
1195
        arm_fpu_arch = FPUTYPE_MAVERICK;
1196
      else
1197
        arm_fpu_arch = FPUTYPE_FPA_EMU2;
1198
#endif
1199
      if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1200
        arm_fpu_tune = FPUTYPE_FPA;
1201
      else
1202
        arm_fpu_tune = arm_fpu_arch;
1203
      arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1204
      gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1205
    }
1206
 
1207
  if (target_float_abi_name != NULL)
1208
    {
1209
      /* The user specified a FP ABI.  */
1210
      for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1211
        {
1212
          if (streq (all_float_abis[i].name, target_float_abi_name))
1213
            {
1214
              arm_float_abi = all_float_abis[i].abi_type;
1215
              break;
1216
            }
1217
        }
1218
      if (i == ARRAY_SIZE (all_float_abis))
1219
        error ("invalid floating point abi: -mfloat-abi=%s",
1220
               target_float_abi_name);
1221
    }
1222
  else
1223
    arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1224
 
1225
  if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1226
    sorry ("-mfloat-abi=hard and VFP");
1227
 
1228
  /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1229
     VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1230
     will ever exist.  GCC makes no attempt to support this combination.  */
1231
  if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1232
    sorry ("iWMMXt and hardware floating point");
1233
 
1234
  /* If soft-float is specified then don't use FPU.  */
1235
  if (TARGET_SOFT_FLOAT)
1236
    arm_fpu_arch = FPUTYPE_NONE;
1237
 
1238
  /* For arm2/3 there is no need to do any scheduling if there is only
1239
     a floating point emulator, or we are doing software floating-point.  */
1240
  if ((TARGET_SOFT_FLOAT
1241
       || arm_fpu_tune == FPUTYPE_FPA_EMU2
1242
       || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1243
      && (tune_flags & FL_MODE32) == 0)
1244
    flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1245
 
1246
  if (target_thread_switch)
1247
    {
1248
      if (strcmp (target_thread_switch, "soft") == 0)
1249
        target_thread_pointer = TP_SOFT;
1250
      else if (strcmp (target_thread_switch, "auto") == 0)
1251
        target_thread_pointer = TP_AUTO;
1252
      else if (strcmp (target_thread_switch, "cp15") == 0)
1253
        target_thread_pointer = TP_CP15;
1254
      else
1255
        error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1256
    }
1257
 
1258
  /* Use the cp15 method if it is available.  */
1259
  if (target_thread_pointer == TP_AUTO)
1260
    {
1261
      if (arm_arch6k && !TARGET_THUMB)
1262
        target_thread_pointer = TP_CP15;
1263
      else
1264
        target_thread_pointer = TP_SOFT;
1265
    }
1266
 
1267
  if (TARGET_HARD_TP && TARGET_THUMB)
1268
    error ("can not use -mtp=cp15 with -mthumb");
1269
 
1270
  /* Override the default structure alignment for AAPCS ABI.  */
1271
  if (TARGET_AAPCS_BASED)
1272
    arm_structure_size_boundary = 8;
1273
 
1274
  if (structure_size_string != NULL)
1275
    {
1276
      int size = strtol (structure_size_string, NULL, 0);
1277
 
1278
      if (size == 8 || size == 32
1279
          || (ARM_DOUBLEWORD_ALIGN && size == 64))
1280
        arm_structure_size_boundary = size;
1281
      else
1282
        warning (0, "structure size boundary can only be set to %s",
1283
                 ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1284
    }
1285
 
1286
  if (arm_pic_register_string != NULL)
1287
    {
1288
      int pic_register = decode_reg_name (arm_pic_register_string);
1289
 
1290
      if (!flag_pic)
1291
        warning (0, "-mpic-register= is useless without -fpic");
1292
 
1293
      /* Prevent the user from choosing an obviously stupid PIC register.  */
1294
      else if (pic_register < 0 || call_used_regs[pic_register]
1295
               || pic_register == HARD_FRAME_POINTER_REGNUM
1296
               || pic_register == STACK_POINTER_REGNUM
1297
               || pic_register >= PC_REGNUM)
1298
        error ("unable to use '%s' for PIC register", arm_pic_register_string);
1299
      else
1300
        arm_pic_register = pic_register;
1301
    }
1302
 
1303
  if (TARGET_THUMB && flag_schedule_insns)
1304
    {
1305
      /* Don't warn since it's on by default in -O2.  */
1306
      flag_schedule_insns = 0;
1307
    }
1308
 
1309
  if (optimize_size)
1310
    {
1311
      arm_constant_limit = 1;
1312
 
1313
      /* If optimizing for size, bump the number of instructions that we
1314
         are prepared to conditionally execute (even on a StrongARM).  */
1315
      max_insns_skipped = 6;
1316
    }
1317
  else
1318
    {
1319
      /* For processors with load scheduling, it never costs more than
1320
         2 cycles to load a constant, and the load scheduler may well
1321
         reduce that to 1.  */
1322
      if (arm_ld_sched)
1323
        arm_constant_limit = 1;
1324
 
1325
      /* On XScale the longer latency of a load makes it more difficult
1326
         to achieve a good schedule, so it's faster to synthesize
1327
         constants that can be done in two insns.  */
1328
      if (arm_tune_xscale)
1329
        arm_constant_limit = 2;
1330
 
1331
      /* StrongARM has early execution of branches, so a sequence
1332
         that is worth skipping is shorter.  */
1333
      if (arm_tune_strongarm)
1334
        max_insns_skipped = 3;
1335
    }
1336
 
1337
  /* Register global variables with the garbage collector.  */
1338
  arm_add_gc_roots ();
1339
}
1340
 
1341
static void
1342
arm_add_gc_roots (void)
1343
{
1344
  gcc_obstack_init(&minipool_obstack);
1345
  minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1346
}
1347
 
1348
/* A table of known ARM exception types.
1349
   For use with the interrupt function attribute.  */
1350
 
1351
typedef struct
1352
{
1353
  const char *const arg;
1354
  const unsigned long return_value;
1355
}
1356
isr_attribute_arg;
1357
 
1358
static const isr_attribute_arg isr_attribute_args [] =
1359
{
1360
  { "IRQ",   ARM_FT_ISR },
1361
  { "irq",   ARM_FT_ISR },
1362
  { "FIQ",   ARM_FT_FIQ },
1363
  { "fiq",   ARM_FT_FIQ },
1364
  { "ABORT", ARM_FT_ISR },
1365
  { "abort", ARM_FT_ISR },
1366
  { "ABORT", ARM_FT_ISR },
1367
  { "abort", ARM_FT_ISR },
1368
  { "UNDEF", ARM_FT_EXCEPTION },
1369
  { "undef", ARM_FT_EXCEPTION },
1370
  { "SWI",   ARM_FT_EXCEPTION },
1371
  { "swi",   ARM_FT_EXCEPTION },
1372
  { NULL,    ARM_FT_NORMAL }
1373
};
1374
 
1375
/* Returns the (interrupt) function type of the current
1376
   function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1377
 
1378
static unsigned long
1379
arm_isr_value (tree argument)
1380
{
1381
  const isr_attribute_arg * ptr;
1382
  const char *              arg;
1383
 
1384
  /* No argument - default to IRQ.  */
1385
  if (argument == NULL_TREE)
1386
    return ARM_FT_ISR;
1387
 
1388
  /* Get the value of the argument.  */
1389
  if (TREE_VALUE (argument) == NULL_TREE
1390
      || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1391
    return ARM_FT_UNKNOWN;
1392
 
1393
  arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1394
 
1395
  /* Check it against the list of known arguments.  */
1396
  for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1397
    if (streq (arg, ptr->arg))
1398
      return ptr->return_value;
1399
 
1400
  /* An unrecognized interrupt type.  */
1401
  return ARM_FT_UNKNOWN;
1402
}
1403
 
1404
/* Computes the type of the current function.  */
1405
 
1406
static unsigned long
1407
arm_compute_func_type (void)
1408
{
1409
  unsigned long type = ARM_FT_UNKNOWN;
1410
  tree a;
1411
  tree attr;
1412
 
1413
  gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1414
 
1415
  /* Decide if the current function is volatile.  Such functions
1416
     never return, and many memory cycles can be saved by not storing
1417
     register values that will never be needed again.  This optimization
1418
     was added to speed up context switching in a kernel application.  */
1419
  if (optimize > 0
1420
      && (TREE_NOTHROW (current_function_decl)
1421
          || !(flag_unwind_tables
1422
               || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1423
      && TREE_THIS_VOLATILE (current_function_decl))
1424
    type |= ARM_FT_VOLATILE;
1425
 
1426
  if (cfun->static_chain_decl != NULL)
1427
    type |= ARM_FT_NESTED;
1428
 
1429
  attr = DECL_ATTRIBUTES (current_function_decl);
1430
 
1431
  a = lookup_attribute ("naked", attr);
1432
  if (a != NULL_TREE)
1433
    type |= ARM_FT_NAKED;
1434
 
1435
  a = lookup_attribute ("isr", attr);
1436
  if (a == NULL_TREE)
1437
    a = lookup_attribute ("interrupt", attr);
1438
 
1439
  if (a == NULL_TREE)
1440
    type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1441
  else
1442
    type |= arm_isr_value (TREE_VALUE (a));
1443
 
1444
  return type;
1445
}
1446
 
1447
/* Returns the type of the current function.  */
1448
 
1449
unsigned long
1450
arm_current_func_type (void)
1451
{
1452
  if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1453
    cfun->machine->func_type = arm_compute_func_type ();
1454
 
1455
  return cfun->machine->func_type;
1456
}
1457
 
1458
/* Return 1 if it is possible to return using a single instruction.
1459
   If SIBLING is non-null, this is a test for a return before a sibling
1460
   call.  SIBLING is the call insn, so we can examine its register usage.  */
1461
 
1462
int
1463
use_return_insn (int iscond, rtx sibling)
1464
{
1465
  int regno;
1466
  unsigned int func_type;
1467
  unsigned long saved_int_regs;
1468
  unsigned HOST_WIDE_INT stack_adjust;
1469
  arm_stack_offsets *offsets;
1470
 
1471
  /* Never use a return instruction before reload has run.  */
1472
  if (!reload_completed)
1473
    return 0;
1474
 
1475
  func_type = arm_current_func_type ();
1476
 
1477
  /* Naked functions and volatile functions need special
1478
     consideration.  */
1479
  if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1480
    return 0;
1481
 
1482
  /* So do interrupt functions that use the frame pointer.  */
1483
  if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1484
    return 0;
1485
 
1486
  offsets = arm_get_frame_offsets ();
1487
  stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1488
 
1489
  /* As do variadic functions.  */
1490
  if (current_function_pretend_args_size
1491
      || cfun->machine->uses_anonymous_args
1492
      /* Or if the function calls __builtin_eh_return () */
1493
      || current_function_calls_eh_return
1494
      /* Or if the function calls alloca */
1495
      || current_function_calls_alloca
1496
      /* Or if there is a stack adjustment.  However, if the stack pointer
1497
         is saved on the stack, we can use a pre-incrementing stack load.  */
1498
      || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1499
    return 0;
1500
 
1501
  saved_int_regs = arm_compute_save_reg_mask ();
1502
 
1503
  /* Unfortunately, the insn
1504
 
1505
       ldmib sp, {..., sp, ...}
1506
 
1507
     triggers a bug on most SA-110 based devices, such that the stack
1508
     pointer won't be correctly restored if the instruction takes a
1509
     page fault.  We work around this problem by popping r3 along with
1510
     the other registers, since that is never slower than executing
1511
     another instruction.
1512
 
1513
     We test for !arm_arch5 here, because code for any architecture
1514
     less than this could potentially be run on one of the buggy
1515
     chips.  */
1516
  if (stack_adjust == 4 && !arm_arch5)
1517
    {
1518
      /* Validate that r3 is a call-clobbered register (always true in
1519
         the default abi) ...  */
1520
      if (!call_used_regs[3])
1521
        return 0;
1522
 
1523
      /* ... that it isn't being used for a return value ... */
1524
      if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1525
        return 0;
1526
 
1527
      /* ... or for a tail-call argument ...  */
1528
      if (sibling)
1529
        {
1530
          gcc_assert (GET_CODE (sibling) == CALL_INSN);
1531
 
1532
          if (find_regno_fusage (sibling, USE, 3))
1533
            return 0;
1534
        }
1535
 
1536
      /* ... and that there are no call-saved registers in r0-r2
1537
         (always true in the default ABI).  */
1538
      if (saved_int_regs & 0x7)
1539
        return 0;
1540
    }
1541
 
1542
  /* Can't be done if interworking with Thumb, and any registers have been
1543
     stacked.  */
1544
  if (TARGET_INTERWORK && saved_int_regs != 0)
1545
    return 0;
1546
 
1547
  /* On StrongARM, conditional returns are expensive if they aren't
1548
     taken and multiple registers have been stacked.  */
1549
  if (iscond && arm_tune_strongarm)
1550
    {
1551
      /* Conditional return when just the LR is stored is a simple
1552
         conditional-load instruction, that's not expensive.  */
1553
      if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1554
        return 0;
1555
 
1556
      if (flag_pic
1557
          && arm_pic_register != INVALID_REGNUM
1558
          && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1559
        return 0;
1560
    }
1561
 
1562
  /* If there are saved registers but the LR isn't saved, then we need
1563
     two instructions for the return.  */
1564
  if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1565
    return 0;
1566
 
1567
  /* Can't be done if any of the FPA regs are pushed,
1568
     since this also requires an insn.  */
1569
  if (TARGET_HARD_FLOAT && TARGET_FPA)
1570
    for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1571
      if (regs_ever_live[regno] && !call_used_regs[regno])
1572
        return 0;
1573
 
1574
  /* Likewise VFP regs.  */
1575
  if (TARGET_HARD_FLOAT && TARGET_VFP)
1576
    for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1577
      if (regs_ever_live[regno] && !call_used_regs[regno])
1578
        return 0;
1579
 
1580
  if (TARGET_REALLY_IWMMXT)
1581
    for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1582
      if (regs_ever_live[regno] && ! call_used_regs [regno])
1583
        return 0;
1584
 
1585
  return 1;
1586
}
1587
 
1588
/* Return TRUE if int I is a valid immediate ARM constant.  */
1589
 
1590
int
1591
const_ok_for_arm (HOST_WIDE_INT i)
1592
{
1593
  int lowbit;
1594
 
1595
  /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1596
     be all zero, or all one.  */
1597
  if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1598
      && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1599
          != ((~(unsigned HOST_WIDE_INT) 0)
1600
              & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1601
    return FALSE;
1602
 
1603
  i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1604
 
1605
  /* Fast return for 0 and small values.  We must do this for zero, since
1606
     the code below can't handle that one case.  */
1607
  if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1608
    return TRUE;
1609
 
1610
  /* Get the number of trailing zeros, rounded down to the nearest even
1611
     number.  */
1612
  lowbit = (ffs ((int) i) - 1) & ~1;
1613
 
1614
  if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1615
    return TRUE;
1616
  else if (lowbit <= 4
1617
           && ((i & ~0xc000003f) == 0
1618
               || (i & ~0xf000000f) == 0
1619
               || (i & ~0xfc000003) == 0))
1620
    return TRUE;
1621
 
1622
  return FALSE;
1623
}
1624
 
1625
/* Return true if I is a valid constant for the operation CODE.  */
1626
static int
1627
const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1628
{
1629
  if (const_ok_for_arm (i))
1630
    return 1;
1631
 
1632
  switch (code)
1633
    {
1634
    case PLUS:
1635
      return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1636
 
1637
    case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1638
    case XOR:
1639
    case IOR:
1640
      return 0;
1641
 
1642
    case AND:
1643
      return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1644
 
1645
    default:
1646
      gcc_unreachable ();
1647
    }
1648
}
1649
 
1650
/* Emit a sequence of insns to handle a large constant.
1651
   CODE is the code of the operation required, it can be any of SET, PLUS,
1652
   IOR, AND, XOR, MINUS;
1653
   MODE is the mode in which the operation is being performed;
1654
   VAL is the integer to operate on;
1655
   SOURCE is the other operand (a register, or a null-pointer for SET);
1656
   SUBTARGETS means it is safe to create scratch registers if that will
1657
   either produce a simpler sequence, or we will want to cse the values.
1658
   Return value is the number of insns emitted.  */
1659
 
1660
int
1661
arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1662
                    HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1663
{
1664
  rtx cond;
1665
 
1666
  if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1667
    cond = COND_EXEC_TEST (PATTERN (insn));
1668
  else
1669
    cond = NULL_RTX;
1670
 
1671
  if (subtargets || code == SET
1672
      || (GET_CODE (target) == REG && GET_CODE (source) == REG
1673
          && REGNO (target) != REGNO (source)))
1674
    {
1675
      /* After arm_reorg has been called, we can't fix up expensive
1676
         constants by pushing them into memory so we must synthesize
1677
         them in-line, regardless of the cost.  This is only likely to
1678
         be more costly on chips that have load delay slots and we are
1679
         compiling without running the scheduler (so no splitting
1680
         occurred before the final instruction emission).
1681
 
1682
         Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1683
      */
1684
      if (!after_arm_reorg
1685
          && !cond
1686
          && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1687
                                1, 0)
1688
              > arm_constant_limit + (code != SET)))
1689
        {
1690
          if (code == SET)
1691
            {
1692
              /* Currently SET is the only monadic value for CODE, all
1693
                 the rest are diadic.  */
1694
              emit_set_insn (target, GEN_INT (val));
1695
              return 1;
1696
            }
1697
          else
1698
            {
1699
              rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1700
 
1701
              emit_set_insn (temp, GEN_INT (val));
1702
              /* For MINUS, the value is subtracted from, since we never
1703
                 have subtraction of a constant.  */
1704
              if (code == MINUS)
1705
                emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1706
              else
1707
                emit_set_insn (target,
1708
                               gen_rtx_fmt_ee (code, mode, source, temp));
1709
              return 2;
1710
            }
1711
        }
1712
    }
1713
 
1714
  return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1715
                           1);
1716
}
1717
 
1718
static int
1719
count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1720
{
1721
  HOST_WIDE_INT temp1;
1722
  int num_insns = 0;
1723
  do
1724
    {
1725
      int end;
1726
 
1727
      if (i <= 0)
1728
        i += 32;
1729
      if (remainder & (3 << (i - 2)))
1730
        {
1731
          end = i - 8;
1732
          if (end < 0)
1733
            end += 32;
1734
          temp1 = remainder & ((0x0ff << end)
1735
                                    | ((i < end) ? (0xff >> (32 - end)) : 0));
1736
          remainder &= ~temp1;
1737
          num_insns++;
1738
          i -= 6;
1739
        }
1740
      i -= 2;
1741
    } while (remainder);
1742
  return num_insns;
1743
}
1744
 
1745
/* Emit an instruction with the indicated PATTERN.  If COND is
1746
   non-NULL, conditionalize the execution of the instruction on COND
1747
   being true.  */
1748
 
1749
static void
1750
emit_constant_insn (rtx cond, rtx pattern)
1751
{
1752
  if (cond)
1753
    pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1754
  emit_insn (pattern);
1755
}
1756
 
1757
/* As above, but extra parameter GENERATE which, if clear, suppresses
1758
   RTL generation.  */
1759
 
1760
static int
1761
arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1762
                  HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1763
                  int generate)
1764
{
1765
  int can_invert = 0;
1766
  int can_negate = 0;
1767
  int can_negate_initial = 0;
1768
  int can_shift = 0;
1769
  int i;
1770
  int num_bits_set = 0;
1771
  int set_sign_bit_copies = 0;
1772
  int clear_sign_bit_copies = 0;
1773
  int clear_zero_bit_copies = 0;
1774
  int set_zero_bit_copies = 0;
1775
  int insns = 0;
1776
  unsigned HOST_WIDE_INT temp1, temp2;
1777
  unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1778
 
1779
  /* Find out which operations are safe for a given CODE.  Also do a quick
1780
     check for degenerate cases; these can occur when DImode operations
1781
     are split.  */
1782
  switch (code)
1783
    {
1784
    case SET:
1785
      can_invert = 1;
1786
      can_shift = 1;
1787
      can_negate = 1;
1788
      break;
1789
 
1790
    case PLUS:
1791
      can_negate = 1;
1792
      can_negate_initial = 1;
1793
      break;
1794
 
1795
    case IOR:
1796
      if (remainder == 0xffffffff)
1797
        {
1798
          if (generate)
1799
            emit_constant_insn (cond,
1800
                                gen_rtx_SET (VOIDmode, target,
1801
                                             GEN_INT (ARM_SIGN_EXTEND (val))));
1802
          return 1;
1803
        }
1804
      if (remainder == 0)
1805
        {
1806
          if (reload_completed && rtx_equal_p (target, source))
1807
            return 0;
1808
          if (generate)
1809
            emit_constant_insn (cond,
1810
                                gen_rtx_SET (VOIDmode, target, source));
1811
          return 1;
1812
        }
1813
      break;
1814
 
1815
    case AND:
1816
      if (remainder == 0)
1817
        {
1818
          if (generate)
1819
            emit_constant_insn (cond,
1820
                                gen_rtx_SET (VOIDmode, target, const0_rtx));
1821
          return 1;
1822
        }
1823
      if (remainder == 0xffffffff)
1824
        {
1825
          if (reload_completed && rtx_equal_p (target, source))
1826
            return 0;
1827
          if (generate)
1828
            emit_constant_insn (cond,
1829
                                gen_rtx_SET (VOIDmode, target, source));
1830
          return 1;
1831
        }
1832
      can_invert = 1;
1833
      break;
1834
 
1835
    case XOR:
1836
      if (remainder == 0)
1837
        {
1838
          if (reload_completed && rtx_equal_p (target, source))
1839
            return 0;
1840
          if (generate)
1841
            emit_constant_insn (cond,
1842
                                gen_rtx_SET (VOIDmode, target, source));
1843
          return 1;
1844
        }
1845
 
1846
      /* We don't know how to handle other cases yet.  */
1847
      gcc_assert (remainder == 0xffffffff);
1848
 
1849
      if (generate)
1850
        emit_constant_insn (cond,
1851
                            gen_rtx_SET (VOIDmode, target,
1852
                                         gen_rtx_NOT (mode, source)));
1853
      return 1;
1854
 
1855
    case MINUS:
1856
      /* We treat MINUS as (val - source), since (source - val) is always
1857
         passed as (source + (-val)).  */
1858
      if (remainder == 0)
1859
        {
1860
          if (generate)
1861
            emit_constant_insn (cond,
1862
                                gen_rtx_SET (VOIDmode, target,
1863
                                             gen_rtx_NEG (mode, source)));
1864
          return 1;
1865
        }
1866
      if (const_ok_for_arm (val))
1867
        {
1868
          if (generate)
1869
            emit_constant_insn (cond,
1870
                                gen_rtx_SET (VOIDmode, target,
1871
                                             gen_rtx_MINUS (mode, GEN_INT (val),
1872
                                                            source)));
1873
          return 1;
1874
        }
1875
      can_negate = 1;
1876
 
1877
      break;
1878
 
1879
    default:
1880
      gcc_unreachable ();
1881
    }
1882
 
1883
  /* If we can do it in one insn get out quickly.  */
1884
  if (const_ok_for_arm (val)
1885
      || (can_negate_initial && const_ok_for_arm (-val))
1886
      || (can_invert && const_ok_for_arm (~val)))
1887
    {
1888
      if (generate)
1889
        emit_constant_insn (cond,
1890
                            gen_rtx_SET (VOIDmode, target,
1891
                                         (source
1892
                                          ? gen_rtx_fmt_ee (code, mode, source,
1893
                                                            GEN_INT (val))
1894
                                          : GEN_INT (val))));
1895
      return 1;
1896
    }
1897
 
1898
  /* Calculate a few attributes that may be useful for specific
1899
     optimizations.  */
1900
  for (i = 31; i >= 0; i--)
1901
    {
1902
      if ((remainder & (1 << i)) == 0)
1903
        clear_sign_bit_copies++;
1904
      else
1905
        break;
1906
    }
1907
 
1908
  for (i = 31; i >= 0; i--)
1909
    {
1910
      if ((remainder & (1 << i)) != 0)
1911
        set_sign_bit_copies++;
1912
      else
1913
        break;
1914
    }
1915
 
1916
  for (i = 0; i <= 31; i++)
1917
    {
1918
      if ((remainder & (1 << i)) == 0)
1919
        clear_zero_bit_copies++;
1920
      else
1921
        break;
1922
    }
1923
 
1924
  for (i = 0; i <= 31; i++)
1925
    {
1926
      if ((remainder & (1 << i)) != 0)
1927
        set_zero_bit_copies++;
1928
      else
1929
        break;
1930
    }
1931
 
1932
  switch (code)
1933
    {
1934
    case SET:
1935
      /* See if we can do this by sign_extending a constant that is known
1936
         to be negative.  This is a good, way of doing it, since the shift
1937
         may well merge into a subsequent insn.  */
1938
      if (set_sign_bit_copies > 1)
1939
        {
1940
          if (const_ok_for_arm
1941
              (temp1 = ARM_SIGN_EXTEND (remainder
1942
                                        << (set_sign_bit_copies - 1))))
1943
            {
1944
              if (generate)
1945
                {
1946
                  rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1947
                  emit_constant_insn (cond,
1948
                                      gen_rtx_SET (VOIDmode, new_src,
1949
                                                   GEN_INT (temp1)));
1950
                  emit_constant_insn (cond,
1951
                                      gen_ashrsi3 (target, new_src,
1952
                                                   GEN_INT (set_sign_bit_copies - 1)));
1953
                }
1954
              return 2;
1955
            }
1956
          /* For an inverted constant, we will need to set the low bits,
1957
             these will be shifted out of harm's way.  */
1958
          temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1959
          if (const_ok_for_arm (~temp1))
1960
            {
1961
              if (generate)
1962
                {
1963
                  rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1964
                  emit_constant_insn (cond,
1965
                                      gen_rtx_SET (VOIDmode, new_src,
1966
                                                   GEN_INT (temp1)));
1967
                  emit_constant_insn (cond,
1968
                                      gen_ashrsi3 (target, new_src,
1969
                                                   GEN_INT (set_sign_bit_copies - 1)));
1970
                }
1971
              return 2;
1972
            }
1973
        }
1974
 
1975
      /* See if we can calculate the value as the difference between two
1976
         valid immediates.  */
1977
      if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1978
        {
1979
          int topshift = clear_sign_bit_copies & ~1;
1980
 
1981
          temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1982
                                   & (0xff000000 >> topshift));
1983
 
1984
          /* If temp1 is zero, then that means the 9 most significant
1985
             bits of remainder were 1 and we've caused it to overflow.
1986
             When topshift is 0 we don't need to do anything since we
1987
             can borrow from 'bit 32'.  */
1988
          if (temp1 == 0 && topshift != 0)
1989
            temp1 = 0x80000000 >> (topshift - 1);
1990
 
1991
          temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1992
 
1993
          if (const_ok_for_arm (temp2))
1994
            {
1995
              if (generate)
1996
                {
1997
                  rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1998
                  emit_constant_insn (cond,
1999
                                      gen_rtx_SET (VOIDmode, new_src,
2000
                                                   GEN_INT (temp1)));
2001
                  emit_constant_insn (cond,
2002
                                      gen_addsi3 (target, new_src,
2003
                                                  GEN_INT (-temp2)));
2004
                }
2005
 
2006
              return 2;
2007
            }
2008
        }
2009
 
2010
      /* See if we can generate this by setting the bottom (or the top)
2011
         16 bits, and then shifting these into the other half of the
2012
         word.  We only look for the simplest cases, to do more would cost
2013
         too much.  Be careful, however, not to generate this when the
2014
         alternative would take fewer insns.  */
2015
      if (val & 0xffff0000)
2016
        {
2017
          temp1 = remainder & 0xffff0000;
2018
          temp2 = remainder & 0x0000ffff;
2019
 
2020
          /* Overlaps outside this range are best done using other methods.  */
2021
          for (i = 9; i < 24; i++)
2022
            {
2023
              if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2024
                  && !const_ok_for_arm (temp2))
2025
                {
2026
                  rtx new_src = (subtargets
2027
                                 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2028
                                 : target);
2029
                  insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2030
                                            source, subtargets, generate);
2031
                  source = new_src;
2032
                  if (generate)
2033
                    emit_constant_insn
2034
                      (cond,
2035
                       gen_rtx_SET
2036
                       (VOIDmode, target,
2037
                        gen_rtx_IOR (mode,
2038
                                     gen_rtx_ASHIFT (mode, source,
2039
                                                     GEN_INT (i)),
2040
                                     source)));
2041
                  return insns + 1;
2042
                }
2043
            }
2044
 
2045
          /* Don't duplicate cases already considered.  */
2046
          for (i = 17; i < 24; i++)
2047
            {
2048
              if (((temp1 | (temp1 >> i)) == remainder)
2049
                  && !const_ok_for_arm (temp1))
2050
                {
2051
                  rtx new_src = (subtargets
2052
                                 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2053
                                 : target);
2054
                  insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2055
                                            source, subtargets, generate);
2056
                  source = new_src;
2057
                  if (generate)
2058
                    emit_constant_insn
2059
                      (cond,
2060
                       gen_rtx_SET (VOIDmode, target,
2061
                                    gen_rtx_IOR
2062
                                    (mode,
2063
                                     gen_rtx_LSHIFTRT (mode, source,
2064
                                                       GEN_INT (i)),
2065
                                     source)));
2066
                  return insns + 1;
2067
                }
2068
            }
2069
        }
2070
      break;
2071
 
2072
    case IOR:
2073
    case XOR:
2074
      /* If we have IOR or XOR, and the constant can be loaded in a
2075
         single instruction, and we can find a temporary to put it in,
2076
         then this can be done in two instructions instead of 3-4.  */
2077
      if (subtargets
2078
          /* TARGET can't be NULL if SUBTARGETS is 0 */
2079
          || (reload_completed && !reg_mentioned_p (target, source)))
2080
        {
2081
          if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2082
            {
2083
              if (generate)
2084
                {
2085
                  rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2086
 
2087
                  emit_constant_insn (cond,
2088
                                      gen_rtx_SET (VOIDmode, sub,
2089
                                                   GEN_INT (val)));
2090
                  emit_constant_insn (cond,
2091
                                      gen_rtx_SET (VOIDmode, target,
2092
                                                   gen_rtx_fmt_ee (code, mode,
2093
                                                                   source, sub)));
2094
                }
2095
              return 2;
2096
            }
2097
        }
2098
 
2099
      if (code == XOR)
2100
        break;
2101
 
2102
      if (set_sign_bit_copies > 8
2103
          && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2104
        {
2105
          if (generate)
2106
            {
2107
              rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2108
              rtx shift = GEN_INT (set_sign_bit_copies);
2109
 
2110
              emit_constant_insn
2111
                (cond,
2112
                 gen_rtx_SET (VOIDmode, sub,
2113
                              gen_rtx_NOT (mode,
2114
                                           gen_rtx_ASHIFT (mode,
2115
                                                           source,
2116
                                                           shift))));
2117
              emit_constant_insn
2118
                (cond,
2119
                 gen_rtx_SET (VOIDmode, target,
2120
                              gen_rtx_NOT (mode,
2121
                                           gen_rtx_LSHIFTRT (mode, sub,
2122
                                                             shift))));
2123
            }
2124
          return 2;
2125
        }
2126
 
2127
      if (set_zero_bit_copies > 8
2128
          && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2129
        {
2130
          if (generate)
2131
            {
2132
              rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2133
              rtx shift = GEN_INT (set_zero_bit_copies);
2134
 
2135
              emit_constant_insn
2136
                (cond,
2137
                 gen_rtx_SET (VOIDmode, sub,
2138
                              gen_rtx_NOT (mode,
2139
                                           gen_rtx_LSHIFTRT (mode,
2140
                                                             source,
2141
                                                             shift))));
2142
              emit_constant_insn
2143
                (cond,
2144
                 gen_rtx_SET (VOIDmode, target,
2145
                              gen_rtx_NOT (mode,
2146
                                           gen_rtx_ASHIFT (mode, sub,
2147
                                                           shift))));
2148
            }
2149
          return 2;
2150
        }
2151
 
2152
      if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2153
        {
2154
          if (generate)
2155
            {
2156
              rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2157
              emit_constant_insn (cond,
2158
                                  gen_rtx_SET (VOIDmode, sub,
2159
                                               gen_rtx_NOT (mode, source)));
2160
              source = sub;
2161
              if (subtargets)
2162
                sub = gen_reg_rtx (mode);
2163
              emit_constant_insn (cond,
2164
                                  gen_rtx_SET (VOIDmode, sub,
2165
                                               gen_rtx_AND (mode, source,
2166
                                                            GEN_INT (temp1))));
2167
              emit_constant_insn (cond,
2168
                                  gen_rtx_SET (VOIDmode, target,
2169
                                               gen_rtx_NOT (mode, sub)));
2170
            }
2171
          return 3;
2172
        }
2173
      break;
2174
 
2175
    case AND:
2176
      /* See if two shifts will do 2 or more insn's worth of work.  */
2177
      if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2178
        {
2179
          HOST_WIDE_INT shift_mask = ((0xffffffff
2180
                                       << (32 - clear_sign_bit_copies))
2181
                                      & 0xffffffff);
2182
 
2183
          if ((remainder | shift_mask) != 0xffffffff)
2184
            {
2185
              if (generate)
2186
                {
2187
                  rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2188
                  insns = arm_gen_constant (AND, mode, cond,
2189
                                            remainder | shift_mask,
2190
                                            new_src, source, subtargets, 1);
2191
                  source = new_src;
2192
                }
2193
              else
2194
                {
2195
                  rtx targ = subtargets ? NULL_RTX : target;
2196
                  insns = arm_gen_constant (AND, mode, cond,
2197
                                            remainder | shift_mask,
2198
                                            targ, source, subtargets, 0);
2199
                }
2200
            }
2201
 
2202
          if (generate)
2203
            {
2204
              rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2205
              rtx shift = GEN_INT (clear_sign_bit_copies);
2206
 
2207
              emit_insn (gen_ashlsi3 (new_src, source, shift));
2208
              emit_insn (gen_lshrsi3 (target, new_src, shift));
2209
            }
2210
 
2211
          return insns + 2;
2212
        }
2213
 
2214
      if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2215
        {
2216
          HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2217
 
2218
          if ((remainder | shift_mask) != 0xffffffff)
2219
            {
2220
              if (generate)
2221
                {
2222
                  rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2223
 
2224
                  insns = arm_gen_constant (AND, mode, cond,
2225
                                            remainder | shift_mask,
2226
                                            new_src, source, subtargets, 1);
2227
                  source = new_src;
2228
                }
2229
              else
2230
                {
2231
                  rtx targ = subtargets ? NULL_RTX : target;
2232
 
2233
                  insns = arm_gen_constant (AND, mode, cond,
2234
                                            remainder | shift_mask,
2235
                                            targ, source, subtargets, 0);
2236
                }
2237
            }
2238
 
2239
          if (generate)
2240
            {
2241
              rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2242
              rtx shift = GEN_INT (clear_zero_bit_copies);
2243
 
2244
              emit_insn (gen_lshrsi3 (new_src, source, shift));
2245
              emit_insn (gen_ashlsi3 (target, new_src, shift));
2246
            }
2247
 
2248
          return insns + 2;
2249
        }
2250
 
2251
      break;
2252
 
2253
    default:
2254
      break;
2255
    }
2256
 
2257
  for (i = 0; i < 32; i++)
2258
    if (remainder & (1 << i))
2259
      num_bits_set++;
2260
 
2261
  if (code == AND || (can_invert && num_bits_set > 16))
2262
    remainder = (~remainder) & 0xffffffff;
2263
  else if (code == PLUS && num_bits_set > 16)
2264
    remainder = (-remainder) & 0xffffffff;
2265
  else
2266
    {
2267
      can_invert = 0;
2268
      can_negate = 0;
2269
    }
2270
 
2271
  /* Now try and find a way of doing the job in either two or three
2272
     instructions.
2273
     We start by looking for the largest block of zeros that are aligned on
2274
     a 2-bit boundary, we then fill up the temps, wrapping around to the
2275
     top of the word when we drop off the bottom.
2276
     In the worst case this code should produce no more than four insns.  */
2277
  {
2278
    int best_start = 0;
2279
    int best_consecutive_zeros = 0;
2280
 
2281
    for (i = 0; i < 32; i += 2)
2282
      {
2283
        int consecutive_zeros = 0;
2284
 
2285
        if (!(remainder & (3 << i)))
2286
          {
2287
            while ((i < 32) && !(remainder & (3 << i)))
2288
              {
2289
                consecutive_zeros += 2;
2290
                i += 2;
2291
              }
2292
            if (consecutive_zeros > best_consecutive_zeros)
2293
              {
2294
                best_consecutive_zeros = consecutive_zeros;
2295
                best_start = i - consecutive_zeros;
2296
              }
2297
            i -= 2;
2298
          }
2299
      }
2300
 
2301
    /* So long as it won't require any more insns to do so, it's
2302
       desirable to emit a small constant (in bits 0...9) in the last
2303
       insn.  This way there is more chance that it can be combined with
2304
       a later addressing insn to form a pre-indexed load or store
2305
       operation.  Consider:
2306
 
2307
               *((volatile int *)0xe0000100) = 1;
2308
               *((volatile int *)0xe0000110) = 2;
2309
 
2310
       We want this to wind up as:
2311
 
2312
                mov rA, #0xe0000000
2313
                mov rB, #1
2314
                str rB, [rA, #0x100]
2315
                mov rB, #2
2316
                str rB, [rA, #0x110]
2317
 
2318
       rather than having to synthesize both large constants from scratch.
2319
 
2320
       Therefore, we calculate how many insns would be required to emit
2321
       the constant starting from `best_start', and also starting from
2322
       zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2323
       yield a shorter sequence, we may as well use zero.  */
2324
    if (best_start != 0
2325
        && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2326
        && (count_insns_for_constant (remainder, 0) <=
2327
            count_insns_for_constant (remainder, best_start)))
2328
      best_start = 0;
2329
 
2330
    /* Now start emitting the insns.  */
2331
    i = best_start;
2332
    do
2333
      {
2334
        int end;
2335
 
2336
        if (i <= 0)
2337
          i += 32;
2338
        if (remainder & (3 << (i - 2)))
2339
          {
2340
            end = i - 8;
2341
            if (end < 0)
2342
              end += 32;
2343
            temp1 = remainder & ((0x0ff << end)
2344
                                 | ((i < end) ? (0xff >> (32 - end)) : 0));
2345
            remainder &= ~temp1;
2346
 
2347
            if (generate)
2348
              {
2349
                rtx new_src, temp1_rtx;
2350
 
2351
                if (code == SET || code == MINUS)
2352
                  {
2353
                    new_src = (subtargets ? gen_reg_rtx (mode) : target);
2354
                    if (can_invert && code != MINUS)
2355
                      temp1 = ~temp1;
2356
                  }
2357
                else
2358
                  {
2359
                    if (remainder && subtargets)
2360
                      new_src = gen_reg_rtx (mode);
2361
                    else
2362
                      new_src = target;
2363
                    if (can_invert)
2364
                      temp1 = ~temp1;
2365
                    else if (can_negate)
2366
                      temp1 = -temp1;
2367
                  }
2368
 
2369
                temp1 = trunc_int_for_mode (temp1, mode);
2370
                temp1_rtx = GEN_INT (temp1);
2371
 
2372
                if (code == SET)
2373
                  ;
2374
                else if (code == MINUS)
2375
                  temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2376
                else
2377
                  temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2378
 
2379
                emit_constant_insn (cond,
2380
                                    gen_rtx_SET (VOIDmode, new_src,
2381
                                                 temp1_rtx));
2382
                source = new_src;
2383
              }
2384
 
2385
            if (code == SET)
2386
              {
2387
                can_invert = 0;
2388
                code = PLUS;
2389
              }
2390
            else if (code == MINUS)
2391
              code = PLUS;
2392
 
2393
            insns++;
2394
            i -= 6;
2395
          }
2396
        i -= 2;
2397
      }
2398
    while (remainder);
2399
  }
2400
 
2401
  return insns;
2402
}
2403
 
2404
/* Canonicalize a comparison so that we are more likely to recognize it.
2405
   This can be done for a few constant compares, where we can make the
2406
   immediate value easier to load.  */
2407
 
2408
enum rtx_code
2409
arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2410
                             rtx * op1)
2411
{
2412
  unsigned HOST_WIDE_INT i = INTVAL (*op1);
2413
  unsigned HOST_WIDE_INT maxval;
2414
  maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2415
 
2416
  switch (code)
2417
    {
2418
    case EQ:
2419
    case NE:
2420
      return code;
2421
 
2422
    case GT:
2423
    case LE:
2424
      if (i != maxval
2425
          && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2426
        {
2427
          *op1 = GEN_INT (i + 1);
2428
          return code == GT ? GE : LT;
2429
        }
2430
      break;
2431
 
2432
    case GE:
2433
    case LT:
2434
      if (i != ~maxval
2435
          && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2436
        {
2437
          *op1 = GEN_INT (i - 1);
2438
          return code == GE ? GT : LE;
2439
        }
2440
      break;
2441
 
2442
    case GTU:
2443
    case LEU:
2444
      if (i != ~((unsigned HOST_WIDE_INT) 0)
2445
          && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2446
        {
2447
          *op1 = GEN_INT (i + 1);
2448
          return code == GTU ? GEU : LTU;
2449
        }
2450
      break;
2451
 
2452
    case GEU:
2453
    case LTU:
2454
      if (i != 0
2455
          && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2456
        {
2457
          *op1 = GEN_INT (i - 1);
2458
          return code == GEU ? GTU : LEU;
2459
        }
2460
      break;
2461
 
2462
    default:
2463
      gcc_unreachable ();
2464
    }
2465
 
2466
  return code;
2467
}
2468
 
2469
 
2470
/* Define how to find the value returned by a function.  */
2471
 
2472
rtx
2473
arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2474
{
2475
  enum machine_mode mode;
2476
  int unsignedp ATTRIBUTE_UNUSED;
2477
  rtx r ATTRIBUTE_UNUSED;
2478
 
2479
  mode = TYPE_MODE (type);
2480
  /* Promote integer types.  */
2481
  if (INTEGRAL_TYPE_P (type))
2482
    PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2483
 
2484
  /* Promotes small structs returned in a register to full-word size
2485
     for big-endian AAPCS.  */
2486
  if (arm_return_in_msb (type))
2487
    {
2488
      HOST_WIDE_INT size = int_size_in_bytes (type);
2489
      if (size % UNITS_PER_WORD != 0)
2490
        {
2491
          size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2492
          mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2493
        }
2494
    }
2495
 
2496
  return LIBCALL_VALUE(mode);
2497
}
2498
 
2499
/* Determine the amount of memory needed to store the possible return
2500
   registers of an untyped call.  */
2501
int
2502
arm_apply_result_size (void)
2503
{
2504
  int size = 16;
2505
 
2506
  if (TARGET_ARM)
2507
    {
2508
      if (TARGET_HARD_FLOAT_ABI)
2509
        {
2510
          if (TARGET_FPA)
2511
            size += 12;
2512
          if (TARGET_MAVERICK)
2513
            size += 8;
2514
        }
2515
      if (TARGET_IWMMXT_ABI)
2516
        size += 8;
2517
    }
2518
 
2519
  return size;
2520
}
2521
 
2522
/* Decide whether a type should be returned in memory (true)
2523
   or in a register (false).  This is called by the macro
2524
   RETURN_IN_MEMORY.  */
2525
int
2526
arm_return_in_memory (tree type)
2527
{
2528
  HOST_WIDE_INT size;
2529
 
2530
  if (!AGGREGATE_TYPE_P (type) &&
2531
      (TREE_CODE (type) != VECTOR_TYPE) &&
2532
      !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2533
    /* All simple types are returned in registers.
2534
       For AAPCS, complex types are treated the same as aggregates.  */
2535
    return 0;
2536
 
2537
  size = int_size_in_bytes (type);
2538
 
2539
  if (arm_abi != ARM_ABI_APCS)
2540
    {
2541
      /* ATPCS and later return aggregate types in memory only if they are
2542
         larger than a word (or are variable size).  */
2543
      return (size < 0 || size > UNITS_PER_WORD);
2544
    }
2545
 
2546
  /* To maximize backwards compatibility with previous versions of gcc,
2547
     return vectors up to 4 words in registers.  */
2548
  if (TREE_CODE (type) == VECTOR_TYPE)
2549
    return (size < 0 || size > (4 * UNITS_PER_WORD));
2550
 
2551
  /* For the arm-wince targets we choose to be compatible with Microsoft's
2552
     ARM and Thumb compilers, which always return aggregates in memory.  */
2553
#ifndef ARM_WINCE
2554
  /* All structures/unions bigger than one word are returned in memory.
2555
     Also catch the case where int_size_in_bytes returns -1.  In this case
2556
     the aggregate is either huge or of variable size, and in either case
2557
     we will want to return it via memory and not in a register.  */
2558
  if (size < 0 || size > UNITS_PER_WORD)
2559
    return 1;
2560
 
2561
  if (TREE_CODE (type) == RECORD_TYPE)
2562
    {
2563
      tree field;
2564
 
2565
      /* For a struct the APCS says that we only return in a register
2566
         if the type is 'integer like' and every addressable element
2567
         has an offset of zero.  For practical purposes this means
2568
         that the structure can have at most one non bit-field element
2569
         and that this element must be the first one in the structure.  */
2570
 
2571
      /* Find the first field, ignoring non FIELD_DECL things which will
2572
         have been created by C++.  */
2573
      for (field = TYPE_FIELDS (type);
2574
           field && TREE_CODE (field) != FIELD_DECL;
2575
           field = TREE_CHAIN (field))
2576
        continue;
2577
 
2578
      if (field == NULL)
2579
        return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2580
 
2581
      /* Check that the first field is valid for returning in a register.  */
2582
 
2583
      /* ... Floats are not allowed */
2584
      if (FLOAT_TYPE_P (TREE_TYPE (field)))
2585
        return 1;
2586
 
2587
      /* ... Aggregates that are not themselves valid for returning in
2588
         a register are not allowed.  */
2589
      if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2590
        return 1;
2591
 
2592
      /* Now check the remaining fields, if any.  Only bitfields are allowed,
2593
         since they are not addressable.  */
2594
      for (field = TREE_CHAIN (field);
2595
           field;
2596
           field = TREE_CHAIN (field))
2597
        {
2598
          if (TREE_CODE (field) != FIELD_DECL)
2599
            continue;
2600
 
2601
          if (!DECL_BIT_FIELD_TYPE (field))
2602
            return 1;
2603
        }
2604
 
2605
      return 0;
2606
    }
2607
 
2608
  if (TREE_CODE (type) == UNION_TYPE)
2609
    {
2610
      tree field;
2611
 
2612
      /* Unions can be returned in registers if every element is
2613
         integral, or can be returned in an integer register.  */
2614
      for (field = TYPE_FIELDS (type);
2615
           field;
2616
           field = TREE_CHAIN (field))
2617
        {
2618
          if (TREE_CODE (field) != FIELD_DECL)
2619
            continue;
2620
 
2621
          if (FLOAT_TYPE_P (TREE_TYPE (field)))
2622
            return 1;
2623
 
2624
          if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2625
            return 1;
2626
        }
2627
 
2628
      return 0;
2629
    }
2630
#endif /* not ARM_WINCE */
2631
 
2632
  /* Return all other types in memory.  */
2633
  return 1;
2634
}
2635
 
2636
/* Indicate whether or not words of a double are in big-endian order.  */
2637
 
2638
int
2639
arm_float_words_big_endian (void)
2640
{
2641
  if (TARGET_MAVERICK)
2642
    return 0;
2643
 
2644
  /* For FPA, float words are always big-endian.  For VFP, floats words
2645
     follow the memory system mode.  */
2646
 
2647
  if (TARGET_FPA)
2648
    {
2649
      return 1;
2650
    }
2651
 
2652
  if (TARGET_VFP)
2653
    return (TARGET_BIG_END ? 1 : 0);
2654
 
2655
  return 1;
2656
}
2657
 
2658
/* Initialize a variable CUM of type CUMULATIVE_ARGS
2659
   for a call to a function whose data type is FNTYPE.
2660
   For a library call, FNTYPE is NULL.  */
2661
void
2662
arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2663
                          rtx libname  ATTRIBUTE_UNUSED,
2664
                          tree fndecl ATTRIBUTE_UNUSED)
2665
{
2666
  /* On the ARM, the offset starts at 0.  */
2667
  pcum->nregs = 0;
2668
  pcum->iwmmxt_nregs = 0;
2669
  pcum->can_split = true;
2670
 
2671
  pcum->call_cookie = CALL_NORMAL;
2672
 
2673
  if (TARGET_LONG_CALLS)
2674
    pcum->call_cookie = CALL_LONG;
2675
 
2676
  /* Check for long call/short call attributes.  The attributes
2677
     override any command line option.  */
2678
  if (fntype)
2679
    {
2680
      if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2681
        pcum->call_cookie = CALL_SHORT;
2682
      else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2683
        pcum->call_cookie = CALL_LONG;
2684
    }
2685
 
2686
  /* Varargs vectors are treated the same as long long.
2687
     named_count avoids having to change the way arm handles 'named' */
2688
  pcum->named_count = 0;
2689
  pcum->nargs = 0;
2690
 
2691
  if (TARGET_REALLY_IWMMXT && fntype)
2692
    {
2693
      tree fn_arg;
2694
 
2695
      for (fn_arg = TYPE_ARG_TYPES (fntype);
2696
           fn_arg;
2697
           fn_arg = TREE_CHAIN (fn_arg))
2698
        pcum->named_count += 1;
2699
 
2700
      if (! pcum->named_count)
2701
        pcum->named_count = INT_MAX;
2702
    }
2703
}
2704
 
2705
 
2706
/* Return true if mode/type need doubleword alignment.  */
2707
bool
2708
arm_needs_doubleword_align (enum machine_mode mode, tree type)
2709
{
2710
  return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2711
          || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2712
}
2713
 
2714
 
2715
/* Determine where to put an argument to a function.
2716
   Value is zero to push the argument on the stack,
2717
   or a hard register in which to store the argument.
2718
 
2719
   MODE is the argument's machine mode.
2720
   TYPE is the data type of the argument (as a tree).
2721
    This is null for libcalls where that information may
2722
    not be available.
2723
   CUM is a variable of type CUMULATIVE_ARGS which gives info about
2724
    the preceding args and about the function being called.
2725
   NAMED is nonzero if this argument is a named parameter
2726
    (otherwise it is an extra parameter matching an ellipsis).  */
2727
 
2728
rtx
2729
arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2730
                  tree type, int named)
2731
{
2732
  int nregs;
2733
 
2734
  /* Varargs vectors are treated the same as long long.
2735
     named_count avoids having to change the way arm handles 'named' */
2736
  if (TARGET_IWMMXT_ABI
2737
      && arm_vector_mode_supported_p (mode)
2738
      && pcum->named_count > pcum->nargs + 1)
2739
    {
2740
      if (pcum->iwmmxt_nregs <= 9)
2741
        return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2742
      else
2743
        {
2744
          pcum->can_split = false;
2745
          return NULL_RTX;
2746
        }
2747
    }
2748
 
2749
  /* Put doubleword aligned quantities in even register pairs.  */
2750
  if (pcum->nregs & 1
2751
      && ARM_DOUBLEWORD_ALIGN
2752
      && arm_needs_doubleword_align (mode, type))
2753
    pcum->nregs++;
2754
 
2755
  if (mode == VOIDmode)
2756
    /* Compute operand 2 of the call insn.  */
2757
    return GEN_INT (pcum->call_cookie);
2758
 
2759
  /* Only allow splitting an arg between regs and memory if all preceding
2760
     args were allocated to regs.  For args passed by reference we only count
2761
     the reference pointer.  */
2762
  if (pcum->can_split)
2763
    nregs = 1;
2764
  else
2765
    nregs = ARM_NUM_REGS2 (mode, type);
2766
 
2767
  if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2768
    return NULL_RTX;
2769
 
2770
  return gen_rtx_REG (mode, pcum->nregs);
2771
}
2772
 
2773
static int
2774
arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2775
                       tree type, bool named ATTRIBUTE_UNUSED)
2776
{
2777
  int nregs = pcum->nregs;
2778
 
2779
  if (arm_vector_mode_supported_p (mode))
2780
    return 0;
2781
 
2782
  if (NUM_ARG_REGS > nregs
2783
      && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2784
      && pcum->can_split)
2785
    return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2786
 
2787
  return 0;
2788
}
2789
 
2790
/* Variable sized types are passed by reference.  This is a GCC
2791
   extension to the ARM ABI.  */
2792
 
2793
static bool
2794
arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2795
                       enum machine_mode mode ATTRIBUTE_UNUSED,
2796
                       tree type, bool named ATTRIBUTE_UNUSED)
2797
{
2798
  return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2799
}
2800
 
2801
/* Encode the current state of the #pragma [no_]long_calls.  */
2802
typedef enum
2803
{
2804
  OFF,          /* No #pragma [no_]long_calls is in effect.  */
2805
  LONG,         /* #pragma long_calls is in effect.  */
2806
  SHORT         /* #pragma no_long_calls is in effect.  */
2807
} arm_pragma_enum;
2808
 
2809
static arm_pragma_enum arm_pragma_long_calls = OFF;
2810
 
2811
void
2812
arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2813
{
2814
  arm_pragma_long_calls = LONG;
2815
}
2816
 
2817
void
2818
arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2819
{
2820
  arm_pragma_long_calls = SHORT;
2821
}
2822
 
2823
void
2824
arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2825
{
2826
  arm_pragma_long_calls = OFF;
2827
}
2828
 
2829
/* Table of machine attributes.  */
2830
const struct attribute_spec arm_attribute_table[] =
2831
{
2832
  /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2833
  /* Function calls made to this symbol must be done indirectly, because
2834
     it may lie outside of the 26 bit addressing range of a normal function
2835
     call.  */
2836
  { "long_call",    0, 0, false, true,  true,  NULL },
2837
  /* Whereas these functions are always known to reside within the 26 bit
2838
     addressing range.  */
2839
  { "short_call",   0, 0, false, true,  true,  NULL },
2840
  /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2841
  { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2842
  { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2843
  { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2844
#ifdef ARM_PE
2845
  /* ARM/PE has three new attributes:
2846
     interfacearm - ?
2847
     dllexport - for exporting a function/variable that will live in a dll
2848
     dllimport - for importing a function/variable from a dll
2849
 
2850
     Microsoft allows multiple declspecs in one __declspec, separating
2851
     them with spaces.  We do NOT support this.  Instead, use __declspec
2852
     multiple times.
2853
  */
2854
  { "dllimport",    0, 0, true,  false, false, NULL },
2855
  { "dllexport",    0, 0, true,  false, false, NULL },
2856
  { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2857
#elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2858
  { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2859
  { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2860
  { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2861
#endif
2862
  { NULL,           0, 0, false, false, false, NULL }
2863
};
2864
 
2865
/* Handle an attribute requiring a FUNCTION_DECL;
2866
   arguments as in struct attribute_spec.handler.  */
2867
static tree
2868
arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2869
                             int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2870
{
2871
  if (TREE_CODE (*node) != FUNCTION_DECL)
2872
    {
2873
      warning (OPT_Wattributes, "%qs attribute only applies to functions",
2874
               IDENTIFIER_POINTER (name));
2875
      *no_add_attrs = true;
2876
    }
2877
 
2878
  return NULL_TREE;
2879
}
2880
 
2881
/* Handle an "interrupt" or "isr" attribute;
2882
   arguments as in struct attribute_spec.handler.  */
2883
static tree
2884
arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2885
                          bool *no_add_attrs)
2886
{
2887
  if (DECL_P (*node))
2888
    {
2889
      if (TREE_CODE (*node) != FUNCTION_DECL)
2890
        {
2891
          warning (OPT_Wattributes, "%qs attribute only applies to functions",
2892
                   IDENTIFIER_POINTER (name));
2893
          *no_add_attrs = true;
2894
        }
2895
      /* FIXME: the argument if any is checked for type attributes;
2896
         should it be checked for decl ones?  */
2897
    }
2898
  else
2899
    {
2900
      if (TREE_CODE (*node) == FUNCTION_TYPE
2901
          || TREE_CODE (*node) == METHOD_TYPE)
2902
        {
2903
          if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2904
            {
2905
              warning (OPT_Wattributes, "%qs attribute ignored",
2906
                       IDENTIFIER_POINTER (name));
2907
              *no_add_attrs = true;
2908
            }
2909
        }
2910
      else if (TREE_CODE (*node) == POINTER_TYPE
2911
               && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2912
                   || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2913
               && arm_isr_value (args) != ARM_FT_UNKNOWN)
2914
        {
2915
          *node = build_variant_type_copy (*node);
2916
          TREE_TYPE (*node) = build_type_attribute_variant
2917
            (TREE_TYPE (*node),
2918
             tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2919
          *no_add_attrs = true;
2920
        }
2921
      else
2922
        {
2923
          /* Possibly pass this attribute on from the type to a decl.  */
2924
          if (flags & ((int) ATTR_FLAG_DECL_NEXT
2925
                       | (int) ATTR_FLAG_FUNCTION_NEXT
2926
                       | (int) ATTR_FLAG_ARRAY_NEXT))
2927
            {
2928
              *no_add_attrs = true;
2929
              return tree_cons (name, args, NULL_TREE);
2930
            }
2931
          else
2932
            {
2933
              warning (OPT_Wattributes, "%qs attribute ignored",
2934
                       IDENTIFIER_POINTER (name));
2935
            }
2936
        }
2937
    }
2938
 
2939
  return NULL_TREE;
2940
}
2941
 
2942
#if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2943
/* Handle the "notshared" attribute.  This attribute is another way of
2944
   requesting hidden visibility.  ARM's compiler supports
2945
   "__declspec(notshared)"; we support the same thing via an
2946
   attribute.  */
2947
 
2948
static tree
2949
arm_handle_notshared_attribute (tree *node,
2950
                                tree name ATTRIBUTE_UNUSED,
2951
                                tree args ATTRIBUTE_UNUSED,
2952
                                int flags ATTRIBUTE_UNUSED,
2953
                                bool *no_add_attrs)
2954
{
2955
  tree decl = TYPE_NAME (*node);
2956
 
2957
  if (decl)
2958
    {
2959
      DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2960
      DECL_VISIBILITY_SPECIFIED (decl) = 1;
2961
      *no_add_attrs = false;
2962
    }
2963
  return NULL_TREE;
2964
}
2965
#endif
2966
 
2967
/* Return 0 if the attributes for two types are incompatible, 1 if they
2968
   are compatible, and 2 if they are nearly compatible (which causes a
2969
   warning to be generated).  */
2970
static int
2971
arm_comp_type_attributes (tree type1, tree type2)
2972
{
2973
  int l1, l2, s1, s2;
2974
 
2975
  /* Check for mismatch of non-default calling convention.  */
2976
  if (TREE_CODE (type1) != FUNCTION_TYPE)
2977
    return 1;
2978
 
2979
  /* Check for mismatched call attributes.  */
2980
  l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2981
  l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2982
  s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2983
  s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2984
 
2985
  /* Only bother to check if an attribute is defined.  */
2986
  if (l1 | l2 | s1 | s2)
2987
    {
2988
      /* If one type has an attribute, the other must have the same attribute.  */
2989
      if ((l1 != l2) || (s1 != s2))
2990
        return 0;
2991
 
2992
      /* Disallow mixed attributes.  */
2993
      if ((l1 & s2) || (l2 & s1))
2994
        return 0;
2995
    }
2996
 
2997
  /* Check for mismatched ISR attribute.  */
2998
  l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2999
  if (! l1)
3000
    l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3001
  l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3002
  if (! l2)
3003
    l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3004
  if (l1 != l2)
3005
    return 0;
3006
 
3007
  return 1;
3008
}
3009
 
3010
/*  Encode long_call or short_call attribute by prefixing
3011
    symbol name in DECL with a special character FLAG.  */
3012
void
3013
arm_encode_call_attribute (tree decl, int flag)
3014
{
3015
  const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
3016
  int          len = strlen (str);
3017
  char *       newstr;
3018
 
3019
  /* Do not allow weak functions to be treated as short call.  */
3020
  if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
3021
    return;
3022
 
3023
  newstr = alloca (len + 2);
3024
  newstr[0] = flag;
3025
  strcpy (newstr + 1, str);
3026
 
3027
  newstr = (char *) ggc_alloc_string (newstr, len + 1);
3028
  XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
3029
}
3030
 
3031
/*  Assigns default attributes to newly defined type.  This is used to
3032
    set short_call/long_call attributes for function types of
3033
    functions defined inside corresponding #pragma scopes.  */
3034
static void
3035
arm_set_default_type_attributes (tree type)
3036
{
3037
  /* Add __attribute__ ((long_call)) to all functions, when
3038
     inside #pragma long_calls or __attribute__ ((short_call)),
3039
     when inside #pragma no_long_calls.  */
3040
  if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3041
    {
3042
      tree type_attr_list, attr_name;
3043
      type_attr_list = TYPE_ATTRIBUTES (type);
3044
 
3045
      if (arm_pragma_long_calls == LONG)
3046
        attr_name = get_identifier ("long_call");
3047
      else if (arm_pragma_long_calls == SHORT)
3048
        attr_name = get_identifier ("short_call");
3049
      else
3050
        return;
3051
 
3052
      type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3053
      TYPE_ATTRIBUTES (type) = type_attr_list;
3054
    }
3055
}
3056
 
3057
/* Return 1 if the operand is a SYMBOL_REF for a function known to be
3058
   defined within the current compilation unit.  If this cannot be
3059
   determined, then 0 is returned.  */
3060
static int
3061
current_file_function_operand (rtx sym_ref)
3062
{
3063
  /* This is a bit of a fib.  A function will have a short call flag
3064
     applied to its name if it has the short call attribute, or it has
3065
     already been defined within the current compilation unit.  */
3066
  if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
3067
    return 1;
3068
 
3069
  /* The current function is always defined within the current compilation
3070
     unit.  If it s a weak definition however, then this may not be the real
3071
     definition of the function, and so we have to say no.  */
3072
  if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3073
      && !DECL_WEAK (current_function_decl))
3074
    return 1;
3075
 
3076
  /* We cannot make the determination - default to returning 0.  */
3077
  return 0;
3078
}
3079
 
3080
/* Return nonzero if a 32 bit "long_call" should be generated for
3081
   this call.  We generate a long_call if the function:
3082
 
3083
        a.  has an __attribute__((long call))
3084
     or b.  is within the scope of a #pragma long_calls
3085
     or c.  the -mlong-calls command line switch has been specified
3086
         .  and either:
3087
                1. -ffunction-sections is in effect
3088
             or 2. the current function has __attribute__ ((section))
3089
             or 3. the target function has __attribute__ ((section))
3090
 
3091
   However we do not generate a long call if the function:
3092
 
3093
        d.  has an __attribute__ ((short_call))
3094
     or e.  is inside the scope of a #pragma no_long_calls
3095
     or f.  is defined within the current compilation unit.
3096
 
3097
   This function will be called by C fragments contained in the machine
3098
   description file.  SYM_REF and CALL_COOKIE correspond to the matched
3099
   rtl operands.  CALL_SYMBOL is used to distinguish between
3100
   two different callers of the function.  It is set to 1 in the
3101
   "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3102
   and "call_value" patterns.  This is because of the difference in the
3103
   SYM_REFs passed by these patterns.  */
3104
int
3105
arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3106
{
3107
  if (!call_symbol)
3108
    {
3109
      if (GET_CODE (sym_ref) != MEM)
3110
        return 0;
3111
 
3112
      sym_ref = XEXP (sym_ref, 0);
3113
    }
3114
 
3115
  if (GET_CODE (sym_ref) != SYMBOL_REF)
3116
    return 0;
3117
 
3118
  if (call_cookie & CALL_SHORT)
3119
    return 0;
3120
 
3121
  if (TARGET_LONG_CALLS)
3122
    {
3123
      if (flag_function_sections
3124
          || DECL_SECTION_NAME (current_function_decl))
3125
        /* c.3 is handled by the definition of the
3126
           ARM_DECLARE_FUNCTION_SIZE macro.  */
3127
        return 1;
3128
    }
3129
 
3130
  if (current_file_function_operand (sym_ref))
3131
    return 0;
3132
 
3133
  return (call_cookie & CALL_LONG)
3134
    || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3135
    || TARGET_LONG_CALLS;
3136
}
3137
 
3138
/* Return nonzero if it is ok to make a tail-call to DECL.  */
3139
static bool
3140
arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3141
{
3142
  int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3143
 
3144
  if (cfun->machine->sibcall_blocked)
3145
    return false;
3146
 
3147
  /* Never tailcall something for which we have no decl, or if we
3148
     are in Thumb mode.  */
3149
  if (decl == NULL || TARGET_THUMB)
3150
    return false;
3151
 
3152
  /* Get the calling method.  */
3153
  if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3154
    call_type = CALL_SHORT;
3155
  else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3156
    call_type = CALL_LONG;
3157
 
3158
  /* Cannot tail-call to long calls, since these are out of range of
3159
     a branch instruction.  However, if not compiling PIC, we know
3160
     we can reach the symbol if it is in this compilation unit.  */
3161
  if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3162
    return false;
3163
 
3164
  /* If we are interworking and the function is not declared static
3165
     then we can't tail-call it unless we know that it exists in this
3166
     compilation unit (since it might be a Thumb routine).  */
3167
  if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3168
    return false;
3169
 
3170
  /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3171
  if (IS_INTERRUPT (arm_current_func_type ()))
3172
    return false;
3173
 
3174
  /* Everything else is ok.  */
3175
  return true;
3176
}
3177
 
3178
 
3179
/* Addressing mode support functions.  */
3180
 
3181
/* Return nonzero if X is a legitimate immediate operand when compiling
3182
   for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3183
int
3184
legitimate_pic_operand_p (rtx x)
3185
{
3186
  if (GET_CODE (x) == SYMBOL_REF
3187
      || (GET_CODE (x) == CONST
3188
          && GET_CODE (XEXP (x, 0)) == PLUS
3189
          && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3190
    return 0;
3191
 
3192
  return 1;
3193
}
3194
 
3195
rtx
3196
legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3197
{
3198
  if (GET_CODE (orig) == SYMBOL_REF
3199
      || GET_CODE (orig) == LABEL_REF)
3200
    {
3201
#ifndef AOF_ASSEMBLER
3202
      rtx pic_ref, address;
3203
#endif
3204
      rtx insn;
3205
      int subregs = 0;
3206
 
3207
      /* If this function doesn't have a pic register, create one now.
3208
         A lot of the logic here is made obscure by the fact that this
3209
         routine gets called as part of the rtx cost estimation
3210
         process.  We don't want those calls to affect any assumptions
3211
         about the real function; and further, we can't call
3212
         entry_of_function() until we start the real expansion
3213
         process.  */
3214
      if (!current_function_uses_pic_offset_table)
3215
        {
3216
          gcc_assert (!no_new_pseudos);
3217
          if (arm_pic_register != INVALID_REGNUM)
3218
            {
3219
              cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3220
 
3221
              /* Play games to avoid marking the function as needing pic
3222
                 if we are being called as part of the cost-estimation
3223
                 process.  */
3224
              if (!ir_type())
3225
                current_function_uses_pic_offset_table = 1;
3226
            }
3227
          else
3228
            {
3229
              rtx seq;
3230
 
3231
              cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3232
 
3233
              /* Play games to avoid marking the function as needing pic
3234
                 if we are being called as part of the cost-estimation
3235
                 process.  */
3236
              if (!ir_type())
3237
                {
3238
                  current_function_uses_pic_offset_table = 1;
3239
                  start_sequence ();
3240
 
3241
                  arm_load_pic_register (0UL);
3242
 
3243
                  seq = get_insns ();
3244
                  end_sequence ();
3245
                  emit_insn_after (seq, entry_of_function ());
3246
                }
3247
            }
3248
        }
3249
 
3250
      if (reg == 0)
3251
        {
3252
          gcc_assert (!no_new_pseudos);
3253
          reg = gen_reg_rtx (Pmode);
3254
 
3255
          subregs = 1;
3256
        }
3257
 
3258
#ifdef AOF_ASSEMBLER
3259
      /* The AOF assembler can generate relocations for these directly, and
3260
         understands that the PIC register has to be added into the offset.  */
3261
      insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3262
#else
3263
      if (subregs)
3264
        address = gen_reg_rtx (Pmode);
3265
      else
3266
        address = reg;
3267
 
3268
      if (TARGET_ARM)
3269
        emit_insn (gen_pic_load_addr_arm (address, orig));
3270
      else
3271
        emit_insn (gen_pic_load_addr_thumb (address, orig));
3272
 
3273
      if ((GET_CODE (orig) == LABEL_REF
3274
           || (GET_CODE (orig) == SYMBOL_REF &&
3275
               SYMBOL_REF_LOCAL_P (orig)))
3276
          && NEED_GOT_RELOC)
3277
        pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3278
      else
3279
        {
3280
          pic_ref = gen_const_mem (Pmode,
3281
                                   gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3282
                                                 address));
3283
        }
3284
 
3285
      insn = emit_move_insn (reg, pic_ref);
3286
#endif
3287
      /* Put a REG_EQUAL note on this insn, so that it can be optimized
3288
         by loop.  */
3289
      REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3290
                                            REG_NOTES (insn));
3291
      return reg;
3292
    }
3293
  else if (GET_CODE (orig) == CONST)
3294
    {
3295
      rtx base, offset;
3296
 
3297
      if (GET_CODE (XEXP (orig, 0)) == PLUS
3298
          && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3299
        return orig;
3300
 
3301
      if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3302
          && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3303
        return orig;
3304
 
3305
      if (reg == 0)
3306
        {
3307
          gcc_assert (!no_new_pseudos);
3308
          reg = gen_reg_rtx (Pmode);
3309
        }
3310
 
3311
      gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3312
 
3313
      base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3314
      offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3315
                                       base == reg ? 0 : reg);
3316
 
3317
      if (GET_CODE (offset) == CONST_INT)
3318
        {
3319
          /* The base register doesn't really matter, we only want to
3320
             test the index for the appropriate mode.  */
3321
          if (!arm_legitimate_index_p (mode, offset, SET, 0))
3322
            {
3323
              gcc_assert (!no_new_pseudos);
3324
              offset = force_reg (Pmode, offset);
3325
            }
3326
 
3327
          if (GET_CODE (offset) == CONST_INT)
3328
            return plus_constant (base, INTVAL (offset));
3329
        }
3330
 
3331
      if (GET_MODE_SIZE (mode) > 4
3332
          && (GET_MODE_CLASS (mode) == MODE_INT
3333
              || TARGET_SOFT_FLOAT))
3334
        {
3335
          emit_insn (gen_addsi3 (reg, base, offset));
3336
          return reg;
3337
        }
3338
 
3339
      return gen_rtx_PLUS (Pmode, base, offset);
3340
    }
3341
 
3342
  return orig;
3343
}
3344
 
3345
 
3346
/* Find a spare low register to use during the prolog of a function.  */
3347
 
3348
static int
3349
thumb_find_work_register (unsigned long pushed_regs_mask)
3350
{
3351
  int reg;
3352
 
3353
  /* Check the argument registers first as these are call-used.  The
3354
     register allocation order means that sometimes r3 might be used
3355
     but earlier argument registers might not, so check them all.  */
3356
  for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3357
    if (!regs_ever_live[reg])
3358
      return reg;
3359
 
3360
  /* Before going on to check the call-saved registers we can try a couple
3361
     more ways of deducing that r3 is available.  The first is when we are
3362
     pushing anonymous arguments onto the stack and we have less than 4
3363
     registers worth of fixed arguments(*).  In this case r3 will be part of
3364
     the variable argument list and so we can be sure that it will be
3365
     pushed right at the start of the function.  Hence it will be available
3366
     for the rest of the prologue.
3367
     (*): ie current_function_pretend_args_size is greater than 0.  */
3368
  if (cfun->machine->uses_anonymous_args
3369
      && current_function_pretend_args_size > 0)
3370
    return LAST_ARG_REGNUM;
3371
 
3372
  /* The other case is when we have fixed arguments but less than 4 registers
3373
     worth.  In this case r3 might be used in the body of the function, but
3374
     it is not being used to convey an argument into the function.  In theory
3375
     we could just check current_function_args_size to see how many bytes are
3376
     being passed in argument registers, but it seems that it is unreliable.
3377
     Sometimes it will have the value 0 when in fact arguments are being
3378
     passed.  (See testcase execute/20021111-1.c for an example).  So we also
3379
     check the args_info.nregs field as well.  The problem with this field is
3380
     that it makes no allowances for arguments that are passed to the
3381
     function but which are not used.  Hence we could miss an opportunity
3382
     when a function has an unused argument in r3.  But it is better to be
3383
     safe than to be sorry.  */
3384
  if (! cfun->machine->uses_anonymous_args
3385
      && current_function_args_size >= 0
3386
      && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3387
      && cfun->args_info.nregs < 4)
3388
    return LAST_ARG_REGNUM;
3389
 
3390
  /* Otherwise look for a call-saved register that is going to be pushed.  */
3391
  for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3392
    if (pushed_regs_mask & (1 << reg))
3393
      return reg;
3394
 
3395
  /* Something went wrong - thumb_compute_save_reg_mask()
3396
     should have arranged for a suitable register to be pushed.  */
3397
  gcc_unreachable ();
3398
}
3399
 
3400
static GTY(()) int pic_labelno;
3401
 
3402
/* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3403
   low register.  */
3404
 
3405
void
3406
arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3407
{
3408
#ifndef AOF_ASSEMBLER
3409
  rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx;
3410
  rtx global_offset_table;
3411
 
3412
  if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3413
    return;
3414
 
3415
  gcc_assert (flag_pic);
3416
 
3417
  /* We use an UNSPEC rather than a LABEL_REF because this label never appears
3418
     in the code stream.  */
3419
 
3420
  labelno = GEN_INT (pic_labelno++);
3421
  l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3422
  l1 = gen_rtx_CONST (VOIDmode, l1);
3423
 
3424
  global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3425
  /* On the ARM the PC register contains 'dot + 8' at the time of the
3426
     addition, on the Thumb it is 'dot + 4'.  */
3427
  pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3428
  if (GOT_PCREL)
3429
    pic_tmp2 = gen_rtx_CONST (VOIDmode,
3430
                            gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3431
  else
3432
    pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3433
 
3434
  pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3435
 
3436
  if (TARGET_ARM)
3437
    {
3438
      emit_insn (gen_pic_load_addr_arm (cfun->machine->pic_reg, pic_rtx));
3439
      emit_insn (gen_pic_add_dot_plus_eight (cfun->machine->pic_reg,
3440
                                             cfun->machine->pic_reg, labelno));
3441
    }
3442
  else
3443
    {
3444
      if (arm_pic_register != INVALID_REGNUM
3445
          && REGNO (cfun->machine->pic_reg) > LAST_LO_REGNUM)
3446
        {
3447
          /* We will have pushed the pic register, so we should always be
3448
             able to find a work register.  */
3449
          pic_tmp = gen_rtx_REG (SImode,
3450
                                 thumb_find_work_register (saved_regs));
3451
          emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3452
          emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3453
        }
3454
      else
3455
        emit_insn (gen_pic_load_addr_thumb (cfun->machine->pic_reg, pic_rtx));
3456
      emit_insn (gen_pic_add_dot_plus_four (cfun->machine->pic_reg,
3457
                                            cfun->machine->pic_reg, labelno));
3458
    }
3459
 
3460
  /* Need to emit this whether or not we obey regdecls,
3461
     since setjmp/longjmp can cause life info to screw up.  */
3462
  emit_insn (gen_rtx_USE (VOIDmode, cfun->machine->pic_reg));
3463
#endif /* AOF_ASSEMBLER */
3464
}
3465
 
3466
 
3467
/* Return nonzero if X is valid as an ARM state addressing register.  */
3468
static int
3469
arm_address_register_rtx_p (rtx x, int strict_p)
3470
{
3471
  int regno;
3472
 
3473
  if (GET_CODE (x) != REG)
3474
    return 0;
3475
 
3476
  regno = REGNO (x);
3477
 
3478
  if (strict_p)
3479
    return ARM_REGNO_OK_FOR_BASE_P (regno);
3480
 
3481
  return (regno <= LAST_ARM_REGNUM
3482
          || regno >= FIRST_PSEUDO_REGISTER
3483
          || regno == FRAME_POINTER_REGNUM
3484
          || regno == ARG_POINTER_REGNUM);
3485
}
3486
 
3487
/* Return TRUE if this rtx is the difference of a symbol and a label,
3488
   and will reduce to a PC-relative relocation in the object file.
3489
   Expressions like this can be left alone when generating PIC, rather
3490
   than forced through the GOT.  */
3491
static int
3492
pcrel_constant_p (rtx x)
3493
{
3494
  if (GET_CODE (x) == MINUS)
3495
    return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3496
 
3497
  return FALSE;
3498
}
3499
 
3500
/* Return nonzero if X is a valid ARM state address operand.  */
3501
int
3502
arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3503
                          int strict_p)
3504
{
3505
  bool use_ldrd;
3506
  enum rtx_code code = GET_CODE (x);
3507
 
3508
  if (arm_address_register_rtx_p (x, strict_p))
3509
    return 1;
3510
 
3511
  use_ldrd = (TARGET_LDRD
3512
              && (mode == DImode
3513
                  || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3514
 
3515
  if (code == POST_INC || code == PRE_DEC
3516
      || ((code == PRE_INC || code == POST_DEC)
3517
          && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3518
    return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3519
 
3520
  else if ((code == POST_MODIFY || code == PRE_MODIFY)
3521
           && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3522
           && GET_CODE (XEXP (x, 1)) == PLUS
3523
           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3524
    {
3525
      rtx addend = XEXP (XEXP (x, 1), 1);
3526
 
3527
      /* Don't allow ldrd post increment by register because it's hard
3528
         to fixup invalid register choices.  */
3529
      if (use_ldrd
3530
          && GET_CODE (x) == POST_MODIFY
3531
          && GET_CODE (addend) == REG)
3532
        return 0;
3533
 
3534
      return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3535
              && arm_legitimate_index_p (mode, addend, outer, strict_p));
3536
    }
3537
 
3538
  /* After reload constants split into minipools will have addresses
3539
     from a LABEL_REF.  */
3540
  else if (reload_completed
3541
           && (code == LABEL_REF
3542
               || (code == CONST
3543
                   && GET_CODE (XEXP (x, 0)) == PLUS
3544
                   && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3545
                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3546
    return 1;
3547
 
3548
  else if (mode == TImode)
3549
    return 0;
3550
 
3551
  else if (code == PLUS)
3552
    {
3553
      rtx xop0 = XEXP (x, 0);
3554
      rtx xop1 = XEXP (x, 1);
3555
 
3556
      return ((arm_address_register_rtx_p (xop0, strict_p)
3557
               && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3558
              || (arm_address_register_rtx_p (xop1, strict_p)
3559
                  && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3560
    }
3561
 
3562
#if 0
3563
  /* Reload currently can't handle MINUS, so disable this for now */
3564
  else if (GET_CODE (x) == MINUS)
3565
    {
3566
      rtx xop0 = XEXP (x, 0);
3567
      rtx xop1 = XEXP (x, 1);
3568
 
3569
      return (arm_address_register_rtx_p (xop0, strict_p)
3570
              && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3571
    }
3572
#endif
3573
 
3574
  else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3575
           && code == SYMBOL_REF
3576
           && CONSTANT_POOL_ADDRESS_P (x)
3577
           && ! (flag_pic
3578
                 && symbol_mentioned_p (get_pool_constant (x))
3579
                 && ! pcrel_constant_p (get_pool_constant (x))))
3580
    return 1;
3581
 
3582
  return 0;
3583
}
3584
 
3585
/* Return nonzero if INDEX is valid for an address index operand in
3586
   ARM state.  */
3587
static int
3588
arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3589
                        int strict_p)
3590
{
3591
  HOST_WIDE_INT range;
3592
  enum rtx_code code = GET_CODE (index);
3593
 
3594
  /* Standard coprocessor addressing modes.  */
3595
  if (TARGET_HARD_FLOAT
3596
      && (TARGET_FPA || TARGET_MAVERICK)
3597
      && (GET_MODE_CLASS (mode) == MODE_FLOAT
3598
          || (TARGET_MAVERICK && mode == DImode)))
3599
    return (code == CONST_INT && INTVAL (index) < 1024
3600
            && INTVAL (index) > -1024
3601
            && (INTVAL (index) & 3) == 0);
3602
 
3603
  if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3604
    {
3605
      /* For DImode assume values will usually live in core regs
3606
         and only allow LDRD addressing modes.  */
3607
      if (!TARGET_LDRD || mode != DImode)
3608
        return (code == CONST_INT
3609
                && INTVAL (index) < 1024
3610
                && INTVAL (index) > -1024
3611
                && (INTVAL (index) & 3) == 0);
3612
    }
3613
 
3614
  if (arm_address_register_rtx_p (index, strict_p)
3615
      && (GET_MODE_SIZE (mode) <= 4))
3616
    return 1;
3617
 
3618
  if (mode == DImode || mode == DFmode)
3619
    {
3620
      if (code == CONST_INT)
3621
        {
3622
          HOST_WIDE_INT val = INTVAL (index);
3623
 
3624
          if (TARGET_LDRD)
3625
            return val > -256 && val < 256;
3626
          else
3627
            return val > -4096 && val < 4092;
3628
        }
3629
 
3630
      return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3631
    }
3632
 
3633
  if (GET_MODE_SIZE (mode) <= 4
3634
      && ! (arm_arch4
3635
            && (mode == HImode
3636
                || (mode == QImode && outer == SIGN_EXTEND))))
3637
    {
3638
      if (code == MULT)
3639
        {
3640
          rtx xiop0 = XEXP (index, 0);
3641
          rtx xiop1 = XEXP (index, 1);
3642
 
3643
          return ((arm_address_register_rtx_p (xiop0, strict_p)
3644
                   && power_of_two_operand (xiop1, SImode))
3645
                  || (arm_address_register_rtx_p (xiop1, strict_p)
3646
                      && power_of_two_operand (xiop0, SImode)));
3647
        }
3648
      else if (code == LSHIFTRT || code == ASHIFTRT
3649
               || code == ASHIFT || code == ROTATERT)
3650
        {
3651
          rtx op = XEXP (index, 1);
3652
 
3653
          return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3654
                  && GET_CODE (op) == CONST_INT
3655
                  && INTVAL (op) > 0
3656
                  && INTVAL (op) <= 31);
3657
        }
3658
    }
3659
 
3660
  /* For ARM v4 we may be doing a sign-extend operation during the
3661
     load.  */
3662
  if (arm_arch4)
3663
    {
3664
      if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3665
        range = 256;
3666
      else
3667
        range = 4096;
3668
    }
3669
  else
3670
    range = (mode == HImode) ? 4095 : 4096;
3671
 
3672
  return (code == CONST_INT
3673
          && INTVAL (index) < range
3674
          && INTVAL (index) > -range);
3675
}
3676
 
3677
/* Return nonzero if X is valid as a Thumb state base register.  */
3678
static int
3679
thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3680
{
3681
  int regno;
3682
 
3683
  if (GET_CODE (x) != REG)
3684
    return 0;
3685
 
3686
  regno = REGNO (x);
3687
 
3688
  if (strict_p)
3689
    return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3690
 
3691
  return (regno <= LAST_LO_REGNUM
3692
          || regno > LAST_VIRTUAL_REGISTER
3693
          || regno == FRAME_POINTER_REGNUM
3694
          || (GET_MODE_SIZE (mode) >= 4
3695
              && (regno == STACK_POINTER_REGNUM
3696
                  || regno >= FIRST_PSEUDO_REGISTER
3697
                  || x == hard_frame_pointer_rtx
3698
                  || x == arg_pointer_rtx)));
3699
}
3700
 
3701
/* Return nonzero if x is a legitimate index register.  This is the case
3702
   for any base register that can access a QImode object.  */
3703
inline static int
3704
thumb_index_register_rtx_p (rtx x, int strict_p)
3705
{
3706
  return thumb_base_register_rtx_p (x, QImode, strict_p);
3707
}
3708
 
3709
/* Return nonzero if x is a legitimate Thumb-state address.
3710
 
3711
   The AP may be eliminated to either the SP or the FP, so we use the
3712
   least common denominator, e.g. SImode, and offsets from 0 to 64.
3713
 
3714
   ??? Verify whether the above is the right approach.
3715
 
3716
   ??? Also, the FP may be eliminated to the SP, so perhaps that
3717
   needs special handling also.
3718
 
3719
   ??? Look at how the mips16 port solves this problem.  It probably uses
3720
   better ways to solve some of these problems.
3721
 
3722
   Although it is not incorrect, we don't accept QImode and HImode
3723
   addresses based on the frame pointer or arg pointer until the
3724
   reload pass starts.  This is so that eliminating such addresses
3725
   into stack based ones won't produce impossible code.  */
3726
int
3727
thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3728
{
3729
  /* ??? Not clear if this is right.  Experiment.  */
3730
  if (GET_MODE_SIZE (mode) < 4
3731
      && !(reload_in_progress || reload_completed)
3732
      && (reg_mentioned_p (frame_pointer_rtx, x)
3733
          || reg_mentioned_p (arg_pointer_rtx, x)
3734
          || reg_mentioned_p (virtual_incoming_args_rtx, x)
3735
          || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3736
          || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3737
          || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3738
    return 0;
3739
 
3740
  /* Accept any base register.  SP only in SImode or larger.  */
3741
  else if (thumb_base_register_rtx_p (x, mode, strict_p))
3742
    return 1;
3743
 
3744
  /* This is PC relative data before arm_reorg runs.  */
3745
  else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3746
           && GET_CODE (x) == SYMBOL_REF
3747
           && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
3748
    return 1;
3749
 
3750
  /* This is PC relative data after arm_reorg runs.  */
3751
  else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3752
           && (GET_CODE (x) == LABEL_REF
3753
               || (GET_CODE (x) == CONST
3754
                   && GET_CODE (XEXP (x, 0)) == PLUS
3755
                   && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3756
                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3757
    return 1;
3758
 
3759
  /* Post-inc indexing only supported for SImode and larger.  */
3760
  else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3761
           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3762
    return 1;
3763
 
3764
  else if (GET_CODE (x) == PLUS)
3765
    {
3766
      /* REG+REG address can be any two index registers.  */
3767
      /* We disallow FRAME+REG addressing since we know that FRAME
3768
         will be replaced with STACK, and SP relative addressing only
3769
         permits SP+OFFSET.  */
3770
      if (GET_MODE_SIZE (mode) <= 4
3771
          && XEXP (x, 0) != frame_pointer_rtx
3772
          && XEXP (x, 1) != frame_pointer_rtx
3773
          && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3774
          && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3775
        return 1;
3776
 
3777
      /* REG+const has 5-7 bit offset for non-SP registers.  */
3778
      else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3779
                || XEXP (x, 0) == arg_pointer_rtx)
3780
               && GET_CODE (XEXP (x, 1)) == CONST_INT
3781
               && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3782
        return 1;
3783
 
3784
      /* REG+const has 10 bit offset for SP, but only SImode and
3785
         larger is supported.  */
3786
      /* ??? Should probably check for DI/DFmode overflow here
3787
         just like GO_IF_LEGITIMATE_OFFSET does.  */
3788
      else if (GET_CODE (XEXP (x, 0)) == REG
3789
               && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3790
               && GET_MODE_SIZE (mode) >= 4
3791
               && GET_CODE (XEXP (x, 1)) == CONST_INT
3792
               && INTVAL (XEXP (x, 1)) >= 0
3793
               && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3794
               && (INTVAL (XEXP (x, 1)) & 3) == 0)
3795
        return 1;
3796
 
3797
      else if (GET_CODE (XEXP (x, 0)) == REG
3798
               && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3799
               && GET_MODE_SIZE (mode) >= 4
3800
               && GET_CODE (XEXP (x, 1)) == CONST_INT
3801
               && (INTVAL (XEXP (x, 1)) & 3) == 0)
3802
        return 1;
3803
    }
3804
 
3805
  else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3806
           && GET_MODE_SIZE (mode) == 4
3807
           && GET_CODE (x) == SYMBOL_REF
3808
           && CONSTANT_POOL_ADDRESS_P (x)
3809
           && ! (flag_pic
3810
                 && symbol_mentioned_p (get_pool_constant (x))
3811
                 && ! pcrel_constant_p (get_pool_constant (x))))
3812
    return 1;
3813
 
3814
  return 0;
3815
}
3816
 
3817
/* Return nonzero if VAL can be used as an offset in a Thumb-state address
3818
   instruction of mode MODE.  */
3819
int
3820
thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3821
{
3822
  switch (GET_MODE_SIZE (mode))
3823
    {
3824
    case 1:
3825
      return val >= 0 && val < 32;
3826
 
3827
    case 2:
3828
      return val >= 0 && val < 64 && (val & 1) == 0;
3829
 
3830
    default:
3831
      return (val >= 0
3832
              && (val + GET_MODE_SIZE (mode)) <= 128
3833
              && (val & 3) == 0);
3834
    }
3835
}
3836
 
3837
/* Build the SYMBOL_REF for __tls_get_addr.  */
3838
 
3839
static GTY(()) rtx tls_get_addr_libfunc;
3840
 
3841
static rtx
3842
get_tls_get_addr (void)
3843
{
3844
  if (!tls_get_addr_libfunc)
3845
    tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
3846
  return tls_get_addr_libfunc;
3847
}
3848
 
3849
static rtx
3850
arm_load_tp (rtx target)
3851
{
3852
  if (!target)
3853
    target = gen_reg_rtx (SImode);
3854
 
3855
  if (TARGET_HARD_TP)
3856
    {
3857
      /* Can return in any reg.  */
3858
      emit_insn (gen_load_tp_hard (target));
3859
    }
3860
  else
3861
    {
3862
      /* Always returned in r0.  Immediately copy the result into a pseudo,
3863
         otherwise other uses of r0 (e.g. setting up function arguments) may
3864
         clobber the value.  */
3865
 
3866
      rtx tmp;
3867
 
3868
      emit_insn (gen_load_tp_soft ());
3869
 
3870
      tmp = gen_rtx_REG (SImode, 0);
3871
      emit_move_insn (target, tmp);
3872
    }
3873
  return target;
3874
}
3875
 
3876
static rtx
3877
load_tls_operand (rtx x, rtx reg)
3878
{
3879
  rtx tmp;
3880
 
3881
  if (reg == NULL_RTX)
3882
    reg = gen_reg_rtx (SImode);
3883
 
3884
  tmp = gen_rtx_CONST (SImode, x);
3885
 
3886
  emit_move_insn (reg, tmp);
3887
 
3888
  return reg;
3889
}
3890
 
3891
static rtx
3892
arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
3893
{
3894
  rtx insns, label, labelno, sum;
3895
 
3896
  start_sequence ();
3897
 
3898
  labelno = GEN_INT (pic_labelno++);
3899
  label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3900
  label = gen_rtx_CONST (VOIDmode, label);
3901
 
3902
  sum = gen_rtx_UNSPEC (Pmode,
3903
                        gen_rtvec (4, x, GEN_INT (reloc), label,
3904
                                   GEN_INT (TARGET_ARM ? 8 : 4)),
3905
                        UNSPEC_TLS);
3906
  reg = load_tls_operand (sum, reg);
3907
 
3908
  if (TARGET_ARM)
3909
    emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
3910
  else
3911
    emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3912
 
3913
  *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
3914
                                     Pmode, 1, reg, Pmode);
3915
 
3916
  insns = get_insns ();
3917
  end_sequence ();
3918
 
3919
  return insns;
3920
}
3921
 
3922
rtx
3923
legitimize_tls_address (rtx x, rtx reg)
3924
{
3925
  rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
3926
  unsigned int model = SYMBOL_REF_TLS_MODEL (x);
3927
 
3928
  switch (model)
3929
    {
3930
    case TLS_MODEL_GLOBAL_DYNAMIC:
3931
      insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
3932
      dest = gen_reg_rtx (Pmode);
3933
      emit_libcall_block (insns, dest, ret, x);
3934
      return dest;
3935
 
3936
    case TLS_MODEL_LOCAL_DYNAMIC:
3937
      insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
3938
 
3939
      /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3940
         share the LDM result with other LD model accesses.  */
3941
      eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
3942
                            UNSPEC_TLS);
3943
      dest = gen_reg_rtx (Pmode);
3944
      emit_libcall_block (insns, dest, ret, eqv);
3945
 
3946
      /* Load the addend.  */
3947
      addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
3948
                               UNSPEC_TLS);
3949
      addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
3950
      return gen_rtx_PLUS (Pmode, dest, addend);
3951
 
3952
    case TLS_MODEL_INITIAL_EXEC:
3953
      labelno = GEN_INT (pic_labelno++);
3954
      label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3955
      label = gen_rtx_CONST (VOIDmode, label);
3956
      sum = gen_rtx_UNSPEC (Pmode,
3957
                            gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
3958
                                       GEN_INT (TARGET_ARM ? 8 : 4)),
3959
                            UNSPEC_TLS);
3960
      reg = load_tls_operand (sum, reg);
3961
 
3962
      if (TARGET_ARM)
3963
        emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
3964
      else
3965
        {
3966
          emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3967
          emit_move_insn (reg, gen_const_mem (SImode, reg));
3968
        }
3969
 
3970
      tp = arm_load_tp (NULL_RTX);
3971
 
3972
      return gen_rtx_PLUS (Pmode, tp, reg);
3973
 
3974
    case TLS_MODEL_LOCAL_EXEC:
3975
      tp = arm_load_tp (NULL_RTX);
3976
 
3977
      reg = gen_rtx_UNSPEC (Pmode,
3978
                            gen_rtvec (2, x, GEN_INT (TLS_LE32)),
3979
                            UNSPEC_TLS);
3980
      reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
3981
 
3982
      return gen_rtx_PLUS (Pmode, tp, reg);
3983
 
3984
    default:
3985
      abort ();
3986
    }
3987
}
3988
 
3989
/* Try machine-dependent ways of modifying an illegitimate address
3990
   to be legitimate.  If we find one, return the new, valid address.  */
3991
rtx
3992
arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3993
{
3994
  if (arm_tls_symbol_p (x))
3995
    return legitimize_tls_address (x, NULL_RTX);
3996
 
3997
  if (GET_CODE (x) == PLUS)
3998
    {
3999
      rtx xop0 = XEXP (x, 0);
4000
      rtx xop1 = XEXP (x, 1);
4001
 
4002
      if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4003
        xop0 = force_reg (SImode, xop0);
4004
 
4005
      if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4006
        xop1 = force_reg (SImode, xop1);
4007
 
4008
      if (ARM_BASE_REGISTER_RTX_P (xop0)
4009
          && GET_CODE (xop1) == CONST_INT)
4010
        {
4011
          HOST_WIDE_INT n, low_n;
4012
          rtx base_reg, val;
4013
          n = INTVAL (xop1);
4014
 
4015
          /* VFP addressing modes actually allow greater offsets, but for
4016
             now we just stick with the lowest common denominator.  */
4017
          if (mode == DImode
4018
              || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4019
            {
4020
              low_n = n & 0x0f;
4021
              n &= ~0x0f;
4022
              if (low_n > 4)
4023
                {
4024
                  n += 16;
4025
                  low_n -= 16;
4026
                }
4027
            }
4028
          else
4029
            {
4030
              low_n = ((mode) == TImode ? 0
4031
                       : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4032
              n -= low_n;
4033
            }
4034
 
4035
          base_reg = gen_reg_rtx (SImode);
4036
          val = force_operand (plus_constant (xop0, n), NULL_RTX);
4037
          emit_move_insn (base_reg, val);
4038
          x = plus_constant (base_reg, low_n);
4039
        }
4040
      else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4041
        x = gen_rtx_PLUS (SImode, xop0, xop1);
4042
    }
4043
 
4044
  /* XXX We don't allow MINUS any more -- see comment in
4045
     arm_legitimate_address_p ().  */
4046
  else if (GET_CODE (x) == MINUS)
4047
    {
4048
      rtx xop0 = XEXP (x, 0);
4049
      rtx xop1 = XEXP (x, 1);
4050
 
4051
      if (CONSTANT_P (xop0))
4052
        xop0 = force_reg (SImode, xop0);
4053
 
4054
      if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4055
        xop1 = force_reg (SImode, xop1);
4056
 
4057
      if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4058
        x = gen_rtx_MINUS (SImode, xop0, xop1);
4059
    }
4060
 
4061
  /* Make sure to take full advantage of the pre-indexed addressing mode
4062
     with absolute addresses which often allows for the base register to
4063
     be factorized for multiple adjacent memory references, and it might
4064
     even allows for the mini pool to be avoided entirely. */
4065
  else if (GET_CODE (x) == CONST_INT && optimize > 0)
4066
    {
4067
      unsigned int bits;
4068
      HOST_WIDE_INT mask, base, index;
4069
      rtx base_reg;
4070
 
4071
      /* ldr and ldrb can use a 12 bit index, ldrsb and the rest can only
4072
         use a 8 bit index. So let's use a 12 bit index for SImode only and
4073
         hope that arm_gen_constant will enable ldrb to use more bits. */
4074
      bits = (mode == SImode) ? 12 : 8;
4075
      mask = (1 << bits) - 1;
4076
      base = INTVAL (x) & ~mask;
4077
      index = INTVAL (x) & mask;
4078
      if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4079
        {
4080
          /* It'll most probably be more efficient to generate the base
4081
             with more bits set and use a negative index instead. */
4082
          base |= mask;
4083
          index -= mask;
4084
        }
4085
      base_reg = force_reg (SImode, GEN_INT (base));
4086
      x = plus_constant (base_reg, index);
4087
    }
4088
 
4089
  if (flag_pic)
4090
    {
4091
      /* We need to find and carefully transform any SYMBOL and LABEL
4092
         references; so go back to the original address expression.  */
4093
      rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4094
 
4095
      if (new_x != orig_x)
4096
        x = new_x;
4097
    }
4098
 
4099
  return x;
4100
}
4101
 
4102
 
4103
/* Try machine-dependent ways of modifying an illegitimate Thumb address
4104
   to be legitimate.  If we find one, return the new, valid address.  */
4105
rtx
4106
thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4107
{
4108
  if (arm_tls_symbol_p (x))
4109
    return legitimize_tls_address (x, NULL_RTX);
4110
 
4111
  if (GET_CODE (x) == PLUS
4112
      && GET_CODE (XEXP (x, 1)) == CONST_INT
4113
      && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4114
          || INTVAL (XEXP (x, 1)) < 0))
4115
    {
4116
      rtx xop0 = XEXP (x, 0);
4117
      rtx xop1 = XEXP (x, 1);
4118
      HOST_WIDE_INT offset = INTVAL (xop1);
4119
 
4120
      /* Try and fold the offset into a biasing of the base register and
4121
         then offsetting that.  Don't do this when optimizing for space
4122
         since it can cause too many CSEs.  */
4123
      if (optimize_size && offset >= 0
4124
          && offset < 256 + 31 * GET_MODE_SIZE (mode))
4125
        {
4126
          HOST_WIDE_INT delta;
4127
 
4128
          if (offset >= 256)
4129
            delta = offset - (256 - GET_MODE_SIZE (mode));
4130
          else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4131
            delta = 31 * GET_MODE_SIZE (mode);
4132
          else
4133
            delta = offset & (~31 * GET_MODE_SIZE (mode));
4134
 
4135
          xop0 = force_operand (plus_constant (xop0, offset - delta),
4136
                                NULL_RTX);
4137
          x = plus_constant (xop0, delta);
4138
        }
4139
      else if (offset < 0 && offset > -256)
4140
        /* Small negative offsets are best done with a subtract before the
4141
           dereference, forcing these into a register normally takes two
4142
           instructions.  */
4143
        x = force_operand (x, NULL_RTX);
4144
      else
4145
        {
4146
          /* For the remaining cases, force the constant into a register.  */
4147
          xop1 = force_reg (SImode, xop1);
4148
          x = gen_rtx_PLUS (SImode, xop0, xop1);
4149
        }
4150
    }
4151
  else if (GET_CODE (x) == PLUS
4152
           && s_register_operand (XEXP (x, 1), SImode)
4153
           && !s_register_operand (XEXP (x, 0), SImode))
4154
    {
4155
      rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4156
 
4157
      x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4158
    }
4159
 
4160
  if (flag_pic)
4161
    {
4162
      /* We need to find and carefully transform any SYMBOL and LABEL
4163
         references; so go back to the original address expression.  */
4164
      rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4165
 
4166
      if (new_x != orig_x)
4167
        x = new_x;
4168
    }
4169
 
4170
  return x;
4171
}
4172
 
4173
rtx
4174
thumb_legitimize_reload_address (rtx *x_p,
4175
                                 enum machine_mode mode,
4176
                                 int opnum, int type,
4177
                                 int ind_levels ATTRIBUTE_UNUSED)
4178
{
4179
  rtx x = *x_p;
4180
 
4181
  if (GET_CODE (x) == PLUS
4182
      && GET_MODE_SIZE (mode) < 4
4183
      && REG_P (XEXP (x, 0))
4184
      && XEXP (x, 0) == stack_pointer_rtx
4185
      && GET_CODE (XEXP (x, 1)) == CONST_INT
4186
      && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4187
    {
4188
      rtx orig_x = x;
4189
 
4190
      x = copy_rtx (x);
4191
      push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4192
                   Pmode, VOIDmode, 0, 0, opnum, type);
4193
      return x;
4194
    }
4195
 
4196
  /* If both registers are hi-regs, then it's better to reload the
4197
     entire expression rather than each register individually.  That
4198
     only requires one reload register rather than two.  */
4199
  if (GET_CODE (x) == PLUS
4200
      && REG_P (XEXP (x, 0))
4201
      && REG_P (XEXP (x, 1))
4202
      && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4203
      && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4204
    {
4205
      rtx orig_x = x;
4206
 
4207
      x = copy_rtx (x);
4208
      push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4209
                   Pmode, VOIDmode, 0, 0, opnum, type);
4210
      return x;
4211
    }
4212
 
4213
  return NULL;
4214
}
4215
 
4216
/* Test for various thread-local symbols.  */
4217
 
4218
/* Return TRUE if X is a thread-local symbol.  */
4219
 
4220
static bool
4221
arm_tls_symbol_p (rtx x)
4222
{
4223
  if (! TARGET_HAVE_TLS)
4224
    return false;
4225
 
4226
  if (GET_CODE (x) != SYMBOL_REF)
4227
    return false;
4228
 
4229
  return SYMBOL_REF_TLS_MODEL (x) != 0;
4230
}
4231
 
4232
/* Helper for arm_tls_referenced_p.  */
4233
 
4234
static int
4235
arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4236
{
4237
  if (GET_CODE (*x) == SYMBOL_REF)
4238
    return SYMBOL_REF_TLS_MODEL (*x) != 0;
4239
 
4240
  /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4241
     TLS offsets, not real symbol references.  */
4242
  if (GET_CODE (*x) == UNSPEC
4243
      && XINT (*x, 1) == UNSPEC_TLS)
4244
    return -1;
4245
 
4246
  return 0;
4247
}
4248
 
4249
/* Return TRUE if X contains any TLS symbol references.  */
4250
 
4251
bool
4252
arm_tls_referenced_p (rtx x)
4253
{
4254
  if (! TARGET_HAVE_TLS)
4255
    return false;
4256
 
4257
  return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4258
}
4259
 
4260
#define REG_OR_SUBREG_REG(X)                                            \
4261
  (GET_CODE (X) == REG                                                  \
4262
   || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4263
 
4264
#define REG_OR_SUBREG_RTX(X)                    \
4265
   (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4266
 
4267
#ifndef COSTS_N_INSNS
4268
#define COSTS_N_INSNS(N) ((N) * 4 - 2)
4269
#endif
4270
static inline int
4271
thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4272
{
4273
  enum machine_mode mode = GET_MODE (x);
4274
 
4275
  switch (code)
4276
    {
4277
    case ASHIFT:
4278
    case ASHIFTRT:
4279
    case LSHIFTRT:
4280
    case ROTATERT:
4281
    case PLUS:
4282
    case MINUS:
4283
    case COMPARE:
4284
    case NEG:
4285
    case NOT:
4286
      return COSTS_N_INSNS (1);
4287
 
4288
    case MULT:
4289
      if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4290
        {
4291
          int cycles = 0;
4292
          unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4293
 
4294
          while (i)
4295
            {
4296
              i >>= 2;
4297
              cycles++;
4298
            }
4299
          return COSTS_N_INSNS (2) + cycles;
4300
        }
4301
      return COSTS_N_INSNS (1) + 16;
4302
 
4303
    case SET:
4304
      return (COSTS_N_INSNS (1)
4305
              + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4306
                     + GET_CODE (SET_DEST (x)) == MEM));
4307
 
4308
    case CONST_INT:
4309
      if (outer == SET)
4310
        {
4311
          if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4312
            return 0;
4313
          if (thumb_shiftable_const (INTVAL (x)))
4314
            return COSTS_N_INSNS (2);
4315
          return COSTS_N_INSNS (3);
4316
        }
4317
      else if ((outer == PLUS || outer == COMPARE)
4318
               && INTVAL (x) < 256 && INTVAL (x) > -256)
4319
        return 0;
4320
      else if (outer == AND
4321
               && INTVAL (x) < 256 && INTVAL (x) >= -256)
4322
        return COSTS_N_INSNS (1);
4323
      else if (outer == ASHIFT || outer == ASHIFTRT
4324
               || outer == LSHIFTRT)
4325
        return 0;
4326
      return COSTS_N_INSNS (2);
4327
 
4328
    case CONST:
4329
    case CONST_DOUBLE:
4330
    case LABEL_REF:
4331
    case SYMBOL_REF:
4332
      return COSTS_N_INSNS (3);
4333
 
4334
    case UDIV:
4335
    case UMOD:
4336
    case DIV:
4337
    case MOD:
4338
      return 100;
4339
 
4340
    case TRUNCATE:
4341
      return 99;
4342
 
4343
    case AND:
4344
    case XOR:
4345
    case IOR:
4346
      /* XXX guess.  */
4347
      return 8;
4348
 
4349
    case MEM:
4350
      /* XXX another guess.  */
4351
      /* Memory costs quite a lot for the first word, but subsequent words
4352
         load at the equivalent of a single insn each.  */
4353
      return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4354
              + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4355
                 ? 4 : 0));
4356
 
4357
    case IF_THEN_ELSE:
4358
      /* XXX a guess.  */
4359
      if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4360
        return 14;
4361
      return 2;
4362
 
4363
    case ZERO_EXTEND:
4364
      /* XXX still guessing.  */
4365
      switch (GET_MODE (XEXP (x, 0)))
4366
        {
4367
        case QImode:
4368
          return (1 + (mode == DImode ? 4 : 0)
4369
                  + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4370
 
4371
        case HImode:
4372
          return (4 + (mode == DImode ? 4 : 0)
4373
                  + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4374
 
4375
        case SImode:
4376
          return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4377
 
4378
        default:
4379
          return 99;
4380
        }
4381
 
4382
    default:
4383
      return 99;
4384
    }
4385
}
4386
 
4387
 
4388
/* Worker routine for arm_rtx_costs.  */
4389
static inline int
4390
arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4391
{
4392
  enum machine_mode mode = GET_MODE (x);
4393
  enum rtx_code subcode;
4394
  int extra_cost;
4395
 
4396
  switch (code)
4397
    {
4398
    case MEM:
4399
      /* Memory costs quite a lot for the first word, but subsequent words
4400
         load at the equivalent of a single insn each.  */
4401
      return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4402
              + (GET_CODE (x) == SYMBOL_REF
4403
                 && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4404
 
4405
    case DIV:
4406
    case MOD:
4407
    case UDIV:
4408
    case UMOD:
4409
      return optimize_size ? COSTS_N_INSNS (2) : 100;
4410
 
4411
    case ROTATE:
4412
      if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4413
        return 4;
4414
      /* Fall through */
4415
    case ROTATERT:
4416
      if (mode != SImode)
4417
        return 8;
4418
      /* Fall through */
4419
    case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4420
      if (mode == DImode)
4421
        return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4422
                + ((GET_CODE (XEXP (x, 0)) == REG
4423
                    || (GET_CODE (XEXP (x, 0)) == SUBREG
4424
                        && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4425
                   ? 0 : 8));
4426
      return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4427
                    || (GET_CODE (XEXP (x, 0)) == SUBREG
4428
                        && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4429
                   ? 0 : 4)
4430
              + ((GET_CODE (XEXP (x, 1)) == REG
4431
                  || (GET_CODE (XEXP (x, 1)) == SUBREG
4432
                      && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4433
                  || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4434
                 ? 0 : 4));
4435
 
4436
    case MINUS:
4437
      if (mode == DImode)
4438
        return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4439
                + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4440
                    || (GET_CODE (XEXP (x, 0)) == CONST_INT
4441
                       && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4442
                   ? 0 : 8));
4443
 
4444
      if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4445
        return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4446
                      || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4447
                          && arm_const_double_rtx (XEXP (x, 1))))
4448
                     ? 0 : 8)
4449
                + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4450
                    || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4451
                        && arm_const_double_rtx (XEXP (x, 0))))
4452
                   ? 0 : 8));
4453
 
4454
      if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4455
            && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4456
            && REG_OR_SUBREG_REG (XEXP (x, 1))))
4457
          || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4458
               || subcode == ASHIFTRT || subcode == LSHIFTRT
4459
               || subcode == ROTATE || subcode == ROTATERT
4460
               || (subcode == MULT
4461
                   && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4462
                   && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4463
                        (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4464
              && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4465
              && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4466
                  || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4467
              && REG_OR_SUBREG_REG (XEXP (x, 0))))
4468
        return 1;
4469
      /* Fall through */
4470
 
4471
    case PLUS:
4472
      if (GET_CODE (XEXP (x, 0)) == MULT)
4473
        {
4474
          extra_cost = rtx_cost (XEXP (x, 0), code);
4475
          if (!REG_OR_SUBREG_REG (XEXP (x, 1)))
4476
            extra_cost += 4 * ARM_NUM_REGS (mode);
4477
          return extra_cost;
4478
        }
4479
 
4480
      if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4481
        return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4482
                + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4483
                    || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4484
                        && arm_const_double_rtx (XEXP (x, 1))))
4485
                   ? 0 : 8));
4486
 
4487
      /* Fall through */
4488
    case AND: case XOR: case IOR:
4489
      extra_cost = 0;
4490
 
4491
      /* Normally the frame registers will be spilt into reg+const during
4492
         reload, so it is a bad idea to combine them with other instructions,
4493
         since then they might not be moved outside of loops.  As a compromise
4494
         we allow integration with ops that have a constant as their second
4495
         operand.  */
4496
      if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4497
           && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4498
           && GET_CODE (XEXP (x, 1)) != CONST_INT)
4499
          || (REG_OR_SUBREG_REG (XEXP (x, 0))
4500
              && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4501
        extra_cost = 4;
4502
 
4503
      if (mode == DImode)
4504
        return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4505
                + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4506
                    || (GET_CODE (XEXP (x, 1)) == CONST_INT
4507
                        && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4508
                   ? 0 : 8));
4509
 
4510
      if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4511
        return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4512
                + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4513
                    || (GET_CODE (XEXP (x, 1)) == CONST_INT
4514
                        && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4515
                   ? 0 : 4));
4516
 
4517
      else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4518
        return (1 + extra_cost
4519
                + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4520
                     || subcode == LSHIFTRT || subcode == ASHIFTRT
4521
                     || subcode == ROTATE || subcode == ROTATERT
4522
                     || (subcode == MULT
4523
                         && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4524
                         && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4525
                              (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4526
                    && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4527
                    && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4528
                        || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4529
                   ? 0 : 4));
4530
 
4531
      return 8;
4532
 
4533
    case MULT:
4534
      /* This should have been handled by the CPU specific routines.  */
4535
      gcc_unreachable ();
4536
 
4537
    case TRUNCATE:
4538
      if (arm_arch3m && mode == SImode
4539
          && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4540
          && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4541
          && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4542
              == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4543
          && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4544
              || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4545
        return 8;
4546
      return 99;
4547
 
4548
    case NEG:
4549
      if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4550
        return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4551
      /* Fall through */
4552
    case NOT:
4553
      if (mode == DImode)
4554
        return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4555
 
4556
      return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4557
 
4558
    case IF_THEN_ELSE:
4559
      if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4560
        return 14;
4561
      return 2;
4562
 
4563
    case COMPARE:
4564
      return 1;
4565
 
4566
    case ABS:
4567
      return 4 + (mode == DImode ? 4 : 0);
4568
 
4569
    case SIGN_EXTEND:
4570
      if (GET_MODE (XEXP (x, 0)) == QImode)
4571
        return (4 + (mode == DImode ? 4 : 0)
4572
                + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4573
      /* Fall through */
4574
    case ZERO_EXTEND:
4575
      switch (GET_MODE (XEXP (x, 0)))
4576
        {
4577
        case QImode:
4578
          return (1 + (mode == DImode ? 4 : 0)
4579
                  + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4580
 
4581
        case HImode:
4582
          return (4 + (mode == DImode ? 4 : 0)
4583
                  + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4584
 
4585
        case SImode:
4586
          return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4587
 
4588
        case V8QImode:
4589
        case V4HImode:
4590
        case V2SImode:
4591
        case V4QImode:
4592
        case V2HImode:
4593
            return 1;
4594
 
4595
        default:
4596
          gcc_unreachable ();
4597
        }
4598
      gcc_unreachable ();
4599
 
4600
    case CONST_INT:
4601
      if (const_ok_for_arm (INTVAL (x)))
4602
        return outer == SET ? 2 : -1;
4603
      else if (outer == AND
4604
               && const_ok_for_arm (~INTVAL (x)))
4605
        return -1;
4606
      else if ((outer == COMPARE
4607
                || outer == PLUS || outer == MINUS)
4608
               && const_ok_for_arm (-INTVAL (x)))
4609
        return -1;
4610
      else
4611
        return 5;
4612
 
4613
    case CONST:
4614
    case LABEL_REF:
4615
    case SYMBOL_REF:
4616
      return 6;
4617
 
4618
    case CONST_DOUBLE:
4619
      if (arm_const_double_rtx (x))
4620
        return outer == SET ? 2 : -1;
4621
      else if ((outer == COMPARE || outer == PLUS)
4622
               && neg_const_double_rtx_ok_for_fpa (x))
4623
        return -1;
4624
      return 7;
4625
 
4626
    default:
4627
      return 99;
4628
    }
4629
}
4630
 
4631
/* RTX costs when optimizing for size.  */
4632
static bool
4633
arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4634
{
4635
  enum machine_mode mode = GET_MODE (x);
4636
 
4637
  if (TARGET_THUMB)
4638
    {
4639
      /* XXX TBD.  For now, use the standard costs.  */
4640
      *total = thumb_rtx_costs (x, code, outer_code);
4641
      return true;
4642
    }
4643
 
4644
  switch (code)
4645
    {
4646
    case MEM:
4647
      /* A memory access costs 1 insn if the mode is small, or the address is
4648
         a single register, otherwise it costs one insn per word.  */
4649
      if (REG_P (XEXP (x, 0)))
4650
        *total = COSTS_N_INSNS (1);
4651
      else
4652
        *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4653
      return true;
4654
 
4655
    case DIV:
4656
    case MOD:
4657
    case UDIV:
4658
    case UMOD:
4659
      /* Needs a libcall, so it costs about this.  */
4660
      *total = COSTS_N_INSNS (2);
4661
      return false;
4662
 
4663
    case ROTATE:
4664
      if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4665
        {
4666
          *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4667
          return true;
4668
        }
4669
      /* Fall through */
4670
    case ROTATERT:
4671
    case ASHIFT:
4672
    case LSHIFTRT:
4673
    case ASHIFTRT:
4674
      if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4675
        {
4676
          *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4677
          return true;
4678
        }
4679
      else if (mode == SImode)
4680
        {
4681
          *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4682
          /* Slightly disparage register shifts, but not by much.  */
4683
          if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4684
            *total += 1 + rtx_cost (XEXP (x, 1), code);
4685
          return true;
4686
        }
4687
 
4688
      /* Needs a libcall.  */
4689
      *total = COSTS_N_INSNS (2);
4690
      return false;
4691
 
4692
    case MINUS:
4693
      if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4694
        {
4695
          *total = COSTS_N_INSNS (1);
4696
          return false;
4697
        }
4698
 
4699
      if (mode == SImode)
4700
        {
4701
          enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4702
          enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4703
 
4704
          if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4705
              || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4706
              || subcode1 == ROTATE || subcode1 == ROTATERT
4707
              || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4708
              || subcode1 == ASHIFTRT)
4709
            {
4710
              /* It's just the cost of the two operands.  */
4711
              *total = 0;
4712
              return false;
4713
            }
4714
 
4715
          *total = COSTS_N_INSNS (1);
4716
          return false;
4717
        }
4718
 
4719
      *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4720
      return false;
4721
 
4722
    case PLUS:
4723
      if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4724
        {
4725
          *total = COSTS_N_INSNS (1);
4726
          return false;
4727
        }
4728
 
4729
      /* Fall through */
4730
    case AND: case XOR: case IOR:
4731
      if (mode == SImode)
4732
        {
4733
          enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4734
 
4735
          if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4736
              || subcode == LSHIFTRT || subcode == ASHIFTRT
4737
              || (code == AND && subcode == NOT))
4738
            {
4739
              /* It's just the cost of the two operands.  */
4740
              *total = 0;
4741
              return false;
4742
            }
4743
        }
4744
 
4745
      *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4746
      return false;
4747
 
4748
    case MULT:
4749
      *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4750
      return false;
4751
 
4752
    case NEG:
4753
      if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4754
        *total = COSTS_N_INSNS (1);
4755
      /* Fall through */
4756
    case NOT:
4757
      *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4758
 
4759
      return false;
4760
 
4761
    case IF_THEN_ELSE:
4762
      *total = 0;
4763
      return false;
4764
 
4765
    case COMPARE:
4766
      if (cc_register (XEXP (x, 0), VOIDmode))
4767
        * total = 0;
4768
      else
4769
        *total = COSTS_N_INSNS (1);
4770
      return false;
4771
 
4772
    case ABS:
4773
      if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4774
        *total = COSTS_N_INSNS (1);
4775
      else
4776
        *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4777
      return false;
4778
 
4779
    case SIGN_EXTEND:
4780
      *total = 0;
4781
      if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4782
        {
4783
          if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4784
            *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4785
        }
4786
      if (mode == DImode)
4787
        *total += COSTS_N_INSNS (1);
4788
      return false;
4789
 
4790
    case ZERO_EXTEND:
4791
      *total = 0;
4792
      if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4793
        {
4794
          switch (GET_MODE (XEXP (x, 0)))
4795
            {
4796
            case QImode:
4797
              *total += COSTS_N_INSNS (1);
4798
              break;
4799
 
4800
            case HImode:
4801
              *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4802
 
4803
            case SImode:
4804
              break;
4805
 
4806
            default:
4807
              *total += COSTS_N_INSNS (2);
4808
            }
4809
        }
4810
 
4811
      if (mode == DImode)
4812
        *total += COSTS_N_INSNS (1);
4813
 
4814
      return false;
4815
 
4816
    case CONST_INT:
4817
      if (const_ok_for_arm (INTVAL (x)))
4818
        *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4819
      else if (const_ok_for_arm (~INTVAL (x)))
4820
        *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4821
      else if (const_ok_for_arm (-INTVAL (x)))
4822
        {
4823
          if (outer_code == COMPARE || outer_code == PLUS
4824
              || outer_code == MINUS)
4825
            *total = 0;
4826
          else
4827
            *total = COSTS_N_INSNS (1);
4828
        }
4829
      else
4830
        *total = COSTS_N_INSNS (2);
4831
      return true;
4832
 
4833
    case CONST:
4834
    case LABEL_REF:
4835
    case SYMBOL_REF:
4836
      *total = COSTS_N_INSNS (2);
4837
      return true;
4838
 
4839
    case CONST_DOUBLE:
4840
      *total = COSTS_N_INSNS (4);
4841
      return true;
4842
 
4843
    default:
4844
      if (mode != VOIDmode)
4845
        *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4846
      else
4847
        *total = COSTS_N_INSNS (4); /* How knows?  */
4848
      return false;
4849
    }
4850
}
4851
 
4852
/* RTX costs for cores with a slow MUL implementation.  */
4853
 
4854
static bool
4855
arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4856
{
4857
  enum machine_mode mode = GET_MODE (x);
4858
 
4859
  if (TARGET_THUMB)
4860
    {
4861
      *total = thumb_rtx_costs (x, code, outer_code);
4862
      return true;
4863
    }
4864
 
4865
  switch (code)
4866
    {
4867
    case MULT:
4868
      if (GET_MODE_CLASS (mode) == MODE_FLOAT
4869
          || mode == DImode)
4870
        {
4871
          *total = 30;
4872
          return true;
4873
        }
4874
 
4875
      if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4876
        {
4877
          unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4878
                                      & (unsigned HOST_WIDE_INT) 0xffffffff);
4879
          int cost, const_ok = const_ok_for_arm (i);
4880
          int j, booth_unit_size;
4881
 
4882
          /* Tune as appropriate.  */
4883
          cost = const_ok ? 4 : 8;
4884
          booth_unit_size = 2;
4885
          for (j = 0; i && j < 32; j += booth_unit_size)
4886
            {
4887
              i >>= booth_unit_size;
4888
              cost += 2;
4889
            }
4890
 
4891
          *total = cost;
4892
          return true;
4893
        }
4894
 
4895
      *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4896
                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4897
      return true;
4898
 
4899
    default:
4900
      *total = arm_rtx_costs_1 (x, code, outer_code);
4901
      return true;
4902
    }
4903
}
4904
 
4905
 
4906
/* RTX cost for cores with a fast multiply unit (M variants).  */
4907
 
4908
static bool
4909
arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4910
{
4911
  enum machine_mode mode = GET_MODE (x);
4912
 
4913
  if (TARGET_THUMB)
4914
    {
4915
      *total = thumb_rtx_costs (x, code, outer_code);
4916
      return true;
4917
    }
4918
 
4919
  switch (code)
4920
    {
4921
    case MULT:
4922
      /* There is no point basing this on the tuning, since it is always the
4923
         fast variant if it exists at all.  */
4924
      if (mode == DImode
4925
          && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4926
          && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4927
              || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4928
        {
4929
          *total = 8;
4930
          return true;
4931
        }
4932
 
4933
 
4934
      if (GET_MODE_CLASS (mode) == MODE_FLOAT
4935
          || mode == DImode)
4936
        {
4937
          *total = 30;
4938
          return true;
4939
        }
4940
 
4941
      if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4942
        {
4943
          unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4944
                                      & (unsigned HOST_WIDE_INT) 0xffffffff);
4945
          int cost, const_ok = const_ok_for_arm (i);
4946
          int j, booth_unit_size;
4947
 
4948
          /* Tune as appropriate.  */
4949
          cost = const_ok ? 4 : 8;
4950
          booth_unit_size = 8;
4951
          for (j = 0; i && j < 32; j += booth_unit_size)
4952
            {
4953
              i >>= booth_unit_size;
4954
              cost += 2;
4955
            }
4956
 
4957
          *total = cost;
4958
          return true;
4959
        }
4960
 
4961
      *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4962
                 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4963
      return true;
4964
 
4965
    default:
4966
      *total = arm_rtx_costs_1 (x, code, outer_code);
4967
      return true;
4968
    }
4969
}
4970
 
4971
 
4972
/* RTX cost for XScale CPUs.  */
4973
 
4974
static bool
4975
arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4976
{
4977
  enum machine_mode mode = GET_MODE (x);
4978
 
4979
  if (TARGET_THUMB)
4980
    {
4981
      *total = thumb_rtx_costs (x, code, outer_code);
4982
      return true;
4983
    }
4984
 
4985
  switch (code)
4986
    {
4987
    case MULT:
4988
      /* There is no point basing this on the tuning, since it is always the
4989
         fast variant if it exists at all.  */
4990
      if (mode == DImode
4991
          && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4992
          && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4993
              || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4994
        {
4995
          *total = 8;
4996
          return true;
4997
        }
4998
 
4999
 
5000
      if (GET_MODE_CLASS (mode) == MODE_FLOAT
5001
          || mode == DImode)
5002
        {
5003
          *total = 30;
5004
          return true;
5005
        }
5006
 
5007
      if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5008
        {
5009
          unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5010
                                      & (unsigned HOST_WIDE_INT) 0xffffffff);
5011
          int cost, const_ok = const_ok_for_arm (i);
5012
          unsigned HOST_WIDE_INT masked_const;
5013
 
5014
          /* The cost will be related to two insns.
5015
             First a load of the constant (MOV or LDR), then a multiply.  */
5016
          cost = 2;
5017
          if (! const_ok)
5018
            cost += 1;      /* LDR is probably more expensive because
5019
                               of longer result latency.  */
5020
          masked_const = i & 0xffff8000;
5021
          if (masked_const != 0 && masked_const != 0xffff8000)
5022
            {
5023
              masked_const = i & 0xf8000000;
5024
              if (masked_const == 0 || masked_const == 0xf8000000)
5025
                cost += 1;
5026
              else
5027
                cost += 2;
5028
            }
5029
          *total = cost;
5030
          return true;
5031
        }
5032
 
5033
      *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5034
                 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5035
      return true;
5036
 
5037
    case COMPARE:
5038
      /* A COMPARE of a MULT is slow on XScale; the muls instruction
5039
         will stall until the multiplication is complete.  */
5040
      if (GET_CODE (XEXP (x, 0)) == MULT)
5041
        *total = 4 + rtx_cost (XEXP (x, 0), code);
5042
      else
5043
        *total = arm_rtx_costs_1 (x, code, outer_code);
5044
      return true;
5045
 
5046
    default:
5047
      *total = arm_rtx_costs_1 (x, code, outer_code);
5048
      return true;
5049
    }
5050
}
5051
 
5052
 
5053
/* RTX costs for 9e (and later) cores.  */
5054
 
5055
static bool
5056
arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5057
{
5058
  enum machine_mode mode = GET_MODE (x);
5059
  int nonreg_cost;
5060
  int cost;
5061
 
5062
  if (TARGET_THUMB)
5063
    {
5064
      switch (code)
5065
        {
5066
        case MULT:
5067
          *total = COSTS_N_INSNS (3);
5068
          return true;
5069
 
5070
        default:
5071
          *total = thumb_rtx_costs (x, code, outer_code);
5072
          return true;
5073
        }
5074
    }
5075
 
5076
  switch (code)
5077
    {
5078
    case MULT:
5079
      /* There is no point basing this on the tuning, since it is always the
5080
         fast variant if it exists at all.  */
5081
      if (mode == DImode
5082
          && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5083
          && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5084
              || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5085
        {
5086
          *total = 3;
5087
          return true;
5088
        }
5089
 
5090
 
5091
      if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5092
        {
5093
          *total = 30;
5094
          return true;
5095
        }
5096
      if (mode == DImode)
5097
        {
5098
          cost = 7;
5099
          nonreg_cost = 8;
5100
        }
5101
      else
5102
        {
5103
          cost = 2;
5104
          nonreg_cost = 4;
5105
        }
5106
 
5107
 
5108
      *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5109
                    + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5110
      return true;
5111
 
5112
    default:
5113
      *total = arm_rtx_costs_1 (x, code, outer_code);
5114
      return true;
5115
    }
5116
}
5117
/* All address computations that can be done are free, but rtx cost returns
5118
   the same for practically all of them.  So we weight the different types
5119
   of address here in the order (most pref first):
5120
   PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5121
static inline int
5122
arm_arm_address_cost (rtx x)
5123
{
5124
  enum rtx_code c  = GET_CODE (x);
5125
 
5126
  if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5127
    return 0;
5128
  if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5129
    return 10;
5130
 
5131
  if (c == PLUS || c == MINUS)
5132
    {
5133
      if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5134
        return 2;
5135
 
5136
      if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5137
        return 3;
5138
 
5139
      return 4;
5140
    }
5141
 
5142
  return 6;
5143
}
5144
 
5145
static inline int
5146
arm_thumb_address_cost (rtx x)
5147
{
5148
  enum rtx_code c  = GET_CODE (x);
5149
 
5150
  if (c == REG)
5151
    return 1;
5152
  if (c == PLUS
5153
      && GET_CODE (XEXP (x, 0)) == REG
5154
      && GET_CODE (XEXP (x, 1)) == CONST_INT)
5155
    return 1;
5156
 
5157
  return 2;
5158
}
5159
 
5160
static int
5161
arm_address_cost (rtx x)
5162
{
5163
  return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5164
}
5165
 
5166
static int
5167
arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5168
{
5169
  rtx i_pat, d_pat;
5170
 
5171
  /* Some true dependencies can have a higher cost depending
5172
     on precisely how certain input operands are used.  */
5173
  if (arm_tune_xscale
5174
      && REG_NOTE_KIND (link) == 0
5175
      && recog_memoized (insn) >= 0
5176
      && recog_memoized (dep) >= 0)
5177
    {
5178
      int shift_opnum = get_attr_shift (insn);
5179
      enum attr_type attr_type = get_attr_type (dep);
5180
 
5181
      /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5182
         operand for INSN.  If we have a shifted input operand and the
5183
         instruction we depend on is another ALU instruction, then we may
5184
         have to account for an additional stall.  */
5185
      if (shift_opnum != 0
5186
          && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5187
        {
5188
          rtx shifted_operand;
5189
          int opno;
5190
 
5191
          /* Get the shifted operand.  */
5192
          extract_insn (insn);
5193
          shifted_operand = recog_data.operand[shift_opnum];
5194
 
5195
          /* Iterate over all the operands in DEP.  If we write an operand
5196
             that overlaps with SHIFTED_OPERAND, then we have increase the
5197
             cost of this dependency.  */
5198
          extract_insn (dep);
5199
          preprocess_constraints ();
5200
          for (opno = 0; opno < recog_data.n_operands; opno++)
5201
            {
5202
              /* We can ignore strict inputs.  */
5203
              if (recog_data.operand_type[opno] == OP_IN)
5204
                continue;
5205
 
5206
              if (reg_overlap_mentioned_p (recog_data.operand[opno],
5207
                                           shifted_operand))
5208
                return 2;
5209
            }
5210
        }
5211
    }
5212
 
5213
  /* XXX This is not strictly true for the FPA.  */
5214
  if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5215
      || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5216
    return 0;
5217
 
5218
  /* Call insns don't incur a stall, even if they follow a load.  */
5219
  if (REG_NOTE_KIND (link) == 0
5220
      && GET_CODE (insn) == CALL_INSN)
5221
    return 1;
5222
 
5223
  if ((i_pat = single_set (insn)) != NULL
5224
      && GET_CODE (SET_SRC (i_pat)) == MEM
5225
      && (d_pat = single_set (dep)) != NULL
5226
      && GET_CODE (SET_DEST (d_pat)) == MEM)
5227
    {
5228
      rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5229
      /* This is a load after a store, there is no conflict if the load reads
5230
         from a cached area.  Assume that loads from the stack, and from the
5231
         constant pool are cached, and that others will miss.  This is a
5232
         hack.  */
5233
 
5234
      if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5235
          || reg_mentioned_p (stack_pointer_rtx, src_mem)
5236
          || reg_mentioned_p (frame_pointer_rtx, src_mem)
5237
          || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5238
        return 1;
5239
    }
5240
 
5241
  return cost;
5242
}
5243
 
5244
static int fp_consts_inited = 0;
5245
 
5246
/* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5247
static const char * const strings_fp[8] =
5248
{
5249
  "0",   "1",   "2",   "3",
5250
  "4",   "5",   "0.5", "10"
5251
};
5252
 
5253
static REAL_VALUE_TYPE values_fp[8];
5254
 
5255
static void
5256
init_fp_table (void)
5257
{
5258
  int i;
5259
  REAL_VALUE_TYPE r;
5260
 
5261
  if (TARGET_VFP)
5262
    fp_consts_inited = 1;
5263
  else
5264
    fp_consts_inited = 8;
5265
 
5266
  for (i = 0; i < fp_consts_inited; i++)
5267
    {
5268
      r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5269
      values_fp[i] = r;
5270
    }
5271
}
5272
 
5273
/* Return TRUE if rtx X is a valid immediate FP constant.  */
5274
int
5275
arm_const_double_rtx (rtx x)
5276
{
5277
  REAL_VALUE_TYPE r;
5278
  int i;
5279
 
5280
  if (!fp_consts_inited)
5281
    init_fp_table ();
5282
 
5283
  REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5284
  if (REAL_VALUE_MINUS_ZERO (r))
5285
    return 0;
5286
 
5287
  for (i = 0; i < fp_consts_inited; i++)
5288
    if (REAL_VALUES_EQUAL (r, values_fp[i]))
5289
      return 1;
5290
 
5291
  return 0;
5292
}
5293
 
5294
/* Return TRUE if rtx X is a valid immediate FPA constant.  */
5295
int
5296
neg_const_double_rtx_ok_for_fpa (rtx x)
5297
{
5298
  REAL_VALUE_TYPE r;
5299
  int i;
5300
 
5301
  if (!fp_consts_inited)
5302
    init_fp_table ();
5303
 
5304
  REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5305
  r = REAL_VALUE_NEGATE (r);
5306
  if (REAL_VALUE_MINUS_ZERO (r))
5307
    return 0;
5308
 
5309
  for (i = 0; i < 8; i++)
5310
    if (REAL_VALUES_EQUAL (r, values_fp[i]))
5311
      return 1;
5312
 
5313
  return 0;
5314
}
5315
 
5316
/* Predicates for `match_operand' and `match_operator'.  */
5317
 
5318
/* Return nonzero if OP is a valid Cirrus memory address pattern.  */
5319
int
5320
cirrus_memory_offset (rtx op)
5321
{
5322
  /* Reject eliminable registers.  */
5323
  if (! (reload_in_progress || reload_completed)
5324
      && (   reg_mentioned_p (frame_pointer_rtx, op)
5325
          || reg_mentioned_p (arg_pointer_rtx, op)
5326
          || reg_mentioned_p (virtual_incoming_args_rtx, op)
5327
          || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5328
          || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5329
          || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5330
    return 0;
5331
 
5332
  if (GET_CODE (op) == MEM)
5333
    {
5334
      rtx ind;
5335
 
5336
      ind = XEXP (op, 0);
5337
 
5338
      /* Match: (mem (reg)).  */
5339
      if (GET_CODE (ind) == REG)
5340
        return 1;
5341
 
5342
      /* Match:
5343
         (mem (plus (reg)
5344
                    (const))).  */
5345
      if (GET_CODE (ind) == PLUS
5346
          && GET_CODE (XEXP (ind, 0)) == REG
5347
          && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5348
          && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5349
        return 1;
5350
    }
5351
 
5352
  return 0;
5353
}
5354
 
5355
/* Return TRUE if OP is a valid coprocessor memory address pattern.
5356
   WB if true if writeback address modes are allowed.  */
5357
 
5358
int
5359
arm_coproc_mem_operand (rtx op, bool wb)
5360
{
5361
  rtx ind;
5362
 
5363
  /* Reject eliminable registers.  */
5364
  if (! (reload_in_progress || reload_completed)
5365
      && (   reg_mentioned_p (frame_pointer_rtx, op)
5366
          || reg_mentioned_p (arg_pointer_rtx, op)
5367
          || reg_mentioned_p (virtual_incoming_args_rtx, op)
5368
          || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5369
          || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5370
          || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5371
    return FALSE;
5372
 
5373
  /* Constants are converted into offsets from labels.  */
5374
  if (GET_CODE (op) != MEM)
5375
    return FALSE;
5376
 
5377
  ind = XEXP (op, 0);
5378
 
5379
  if (reload_completed
5380
      && (GET_CODE (ind) == LABEL_REF
5381
          || (GET_CODE (ind) == CONST
5382
              && GET_CODE (XEXP (ind, 0)) == PLUS
5383
              && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5384
              && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5385
    return TRUE;
5386
 
5387
  /* Match: (mem (reg)).  */
5388
  if (GET_CODE (ind) == REG)
5389
    return arm_address_register_rtx_p (ind, 0);
5390
 
5391
  /* Autoincremment addressing modes.  */
5392
  if (wb
5393
      && (GET_CODE (ind) == PRE_INC
5394
          || GET_CODE (ind) == POST_INC
5395
          || GET_CODE (ind) == PRE_DEC
5396
          || GET_CODE (ind) == POST_DEC))
5397
    return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5398
 
5399
  if (wb
5400
      && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5401
      && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5402
      && GET_CODE (XEXP (ind, 1)) == PLUS
5403
      && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5404
    ind = XEXP (ind, 1);
5405
 
5406
  /* Match:
5407
     (plus (reg)
5408
           (const)).  */
5409
  if (GET_CODE (ind) == PLUS
5410
      && GET_CODE (XEXP (ind, 0)) == REG
5411
      && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5412
      && GET_CODE (XEXP (ind, 1)) == CONST_INT
5413
      && INTVAL (XEXP (ind, 1)) > -1024
5414
      && INTVAL (XEXP (ind, 1)) <  1024
5415
      && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5416
    return TRUE;
5417
 
5418
  return FALSE;
5419
}
5420
 
5421
/* Return true if X is a register that will be eliminated later on.  */
5422
int
5423
arm_eliminable_register (rtx x)
5424
{
5425
  return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5426
                       || REGNO (x) == ARG_POINTER_REGNUM
5427
                       || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5428
                           && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5429
}
5430
 
5431
/* Return GENERAL_REGS if a scratch register required to reload x to/from
5432
   coprocessor registers.  Otherwise return NO_REGS.  */
5433
 
5434
enum reg_class
5435
coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
5436
{
5437
  if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
5438
    return NO_REGS;
5439
 
5440
  return GENERAL_REGS;
5441
}
5442
 
5443
/* Values which must be returned in the most-significant end of the return
5444
   register.  */
5445
 
5446
static bool
5447
arm_return_in_msb (tree valtype)
5448
{
5449
  return (TARGET_AAPCS_BASED
5450
          && BYTES_BIG_ENDIAN
5451
          && (AGGREGATE_TYPE_P (valtype)
5452
              || TREE_CODE (valtype) == COMPLEX_TYPE));
5453
}
5454
 
5455
/* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5456
   Use by the Cirrus Maverick code which has to workaround
5457
   a hardware bug triggered by such instructions.  */
5458
static bool
5459
arm_memory_load_p (rtx insn)
5460
{
5461
  rtx body, lhs, rhs;;
5462
 
5463
  if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5464
    return false;
5465
 
5466
  body = PATTERN (insn);
5467
 
5468
  if (GET_CODE (body) != SET)
5469
    return false;
5470
 
5471
  lhs = XEXP (body, 0);
5472
  rhs = XEXP (body, 1);
5473
 
5474
  lhs = REG_OR_SUBREG_RTX (lhs);
5475
 
5476
  /* If the destination is not a general purpose
5477
     register we do not have to worry.  */
5478
  if (GET_CODE (lhs) != REG
5479
      || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5480
    return false;
5481
 
5482
  /* As well as loads from memory we also have to react
5483
     to loads of invalid constants which will be turned
5484
     into loads from the minipool.  */
5485
  return (GET_CODE (rhs) == MEM
5486
          || GET_CODE (rhs) == SYMBOL_REF
5487
          || note_invalid_constants (insn, -1, false));
5488
}
5489
 
5490
/* Return TRUE if INSN is a Cirrus instruction.  */
5491
static bool
5492
arm_cirrus_insn_p (rtx insn)
5493
{
5494
  enum attr_cirrus attr;
5495
 
5496
  /* get_attr cannot accept USE or CLOBBER.  */
5497
  if (!insn
5498
      || GET_CODE (insn) != INSN
5499
      || GET_CODE (PATTERN (insn)) == USE
5500
      || GET_CODE (PATTERN (insn)) == CLOBBER)
5501
    return 0;
5502
 
5503
  attr = get_attr_cirrus (insn);
5504
 
5505
  return attr != CIRRUS_NOT;
5506
}
5507
 
5508
/* Cirrus reorg for invalid instruction combinations.  */
5509
static void
5510
cirrus_reorg (rtx first)
5511
{
5512
  enum attr_cirrus attr;
5513
  rtx body = PATTERN (first);
5514
  rtx t;
5515
  int nops;
5516
 
5517
  /* Any branch must be followed by 2 non Cirrus instructions.  */
5518
  if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5519
    {
5520
      nops = 0;
5521
      t = next_nonnote_insn (first);
5522
 
5523
      if (arm_cirrus_insn_p (t))
5524
        ++ nops;
5525
 
5526
      if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5527
        ++ nops;
5528
 
5529
      while (nops --)
5530
        emit_insn_after (gen_nop (), first);
5531
 
5532
      return;
5533
    }
5534
 
5535
  /* (float (blah)) is in parallel with a clobber.  */
5536
  if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5537
    body = XVECEXP (body, 0, 0);
5538
 
5539
  if (GET_CODE (body) == SET)
5540
    {
5541
      rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5542
 
5543
      /* cfldrd, cfldr64, cfstrd, cfstr64 must
5544
         be followed by a non Cirrus insn.  */
5545
      if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5546
        {
5547
          if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5548
            emit_insn_after (gen_nop (), first);
5549
 
5550
          return;
5551
        }
5552
      else if (arm_memory_load_p (first))
5553
        {
5554
          unsigned int arm_regno;
5555
 
5556
          /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5557
             ldr/cfmv64hr combination where the Rd field is the same
5558
             in both instructions must be split with a non Cirrus
5559
             insn.  Example:
5560
 
5561
             ldr r0, blah
5562
             nop
5563
             cfmvsr mvf0, r0.  */
5564
 
5565
          /* Get Arm register number for ldr insn.  */
5566
          if (GET_CODE (lhs) == REG)
5567
            arm_regno = REGNO (lhs);
5568
          else
5569
            {
5570
              gcc_assert (GET_CODE (rhs) == REG);
5571
              arm_regno = REGNO (rhs);
5572
            }
5573
 
5574
          /* Next insn.  */
5575
          first = next_nonnote_insn (first);
5576
 
5577
          if (! arm_cirrus_insn_p (first))
5578
            return;
5579
 
5580
          body = PATTERN (first);
5581
 
5582
          /* (float (blah)) is in parallel with a clobber.  */
5583
          if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5584
            body = XVECEXP (body, 0, 0);
5585
 
5586
          if (GET_CODE (body) == FLOAT)
5587
            body = XEXP (body, 0);
5588
 
5589
          if (get_attr_cirrus (first) == CIRRUS_MOVE
5590
              && GET_CODE (XEXP (body, 1)) == REG
5591
              && arm_regno == REGNO (XEXP (body, 1)))
5592
            emit_insn_after (gen_nop (), first);
5593
 
5594
          return;
5595
        }
5596
    }
5597
 
5598
  /* get_attr cannot accept USE or CLOBBER.  */
5599
  if (!first
5600
      || GET_CODE (first) != INSN
5601
      || GET_CODE (PATTERN (first)) == USE
5602
      || GET_CODE (PATTERN (first)) == CLOBBER)
5603
    return;
5604
 
5605
  attr = get_attr_cirrus (first);
5606
 
5607
  /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5608
     must be followed by a non-coprocessor instruction.  */
5609
  if (attr == CIRRUS_COMPARE)
5610
    {
5611
      nops = 0;
5612
 
5613
      t = next_nonnote_insn (first);
5614
 
5615
      if (arm_cirrus_insn_p (t))
5616
        ++ nops;
5617
 
5618
      if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5619
        ++ nops;
5620
 
5621
      while (nops --)
5622
        emit_insn_after (gen_nop (), first);
5623
 
5624
      return;
5625
    }
5626
}
5627
 
5628
/* Return TRUE if X references a SYMBOL_REF.  */
5629
int
5630
symbol_mentioned_p (rtx x)
5631
{
5632
  const char * fmt;
5633
  int i;
5634
 
5635
  if (GET_CODE (x) == SYMBOL_REF)
5636
    return 1;
5637
 
5638
  /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
5639
     are constant offsets, not symbols.  */
5640
  if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5641
    return 0;
5642
 
5643
  fmt = GET_RTX_FORMAT (GET_CODE (x));
5644
 
5645
  for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5646
    {
5647
      if (fmt[i] == 'E')
5648
        {
5649
          int j;
5650
 
5651
          for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5652
            if (symbol_mentioned_p (XVECEXP (x, i, j)))
5653
              return 1;
5654
        }
5655
      else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5656
        return 1;
5657
    }
5658
 
5659
  return 0;
5660
}
5661
 
5662
/* Return TRUE if X references a LABEL_REF.  */
5663
int
5664
label_mentioned_p (rtx x)
5665
{
5666
  const char * fmt;
5667
  int i;
5668
 
5669
  if (GET_CODE (x) == LABEL_REF)
5670
    return 1;
5671
 
5672
  /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
5673
     instruction, but they are constant offsets, not symbols.  */
5674
  if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5675
    return 0;
5676
 
5677
  fmt = GET_RTX_FORMAT (GET_CODE (x));
5678
  for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5679
    {
5680
      if (fmt[i] == 'E')
5681
        {
5682
          int j;
5683
 
5684
          for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5685
            if (label_mentioned_p (XVECEXP (x, i, j)))
5686
              return 1;
5687
        }
5688
      else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5689
        return 1;
5690
    }
5691
 
5692
  return 0;
5693
}
5694
 
5695
int
5696
tls_mentioned_p (rtx x)
5697
{
5698
  switch (GET_CODE (x))
5699
    {
5700
    case CONST:
5701
      return tls_mentioned_p (XEXP (x, 0));
5702
 
5703
    case UNSPEC:
5704
      if (XINT (x, 1) == UNSPEC_TLS)
5705
        return 1;
5706
 
5707
    default:
5708
      return 0;
5709
    }
5710
}
5711
 
5712
/* Must not copy a SET whose source operand is PC-relative.  */
5713
 
5714
static bool
5715
arm_cannot_copy_insn_p (rtx insn)
5716
{
5717
  rtx pat = PATTERN (insn);
5718
 
5719
  if (GET_CODE (pat) == PARALLEL
5720
      && GET_CODE (XVECEXP (pat, 0, 0)) == SET)
5721
    {
5722
      rtx rhs = SET_SRC (XVECEXP (pat, 0, 0));
5723
 
5724
      if (GET_CODE (rhs) == UNSPEC
5725
          && XINT (rhs, 1) == UNSPEC_PIC_BASE)
5726
        return TRUE;
5727
 
5728
      if (GET_CODE (rhs) == MEM
5729
          && GET_CODE (XEXP (rhs, 0)) == UNSPEC
5730
          && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
5731
        return TRUE;
5732
    }
5733
 
5734
  return FALSE;
5735
}
5736
 
5737
enum rtx_code
5738
minmax_code (rtx x)
5739
{
5740
  enum rtx_code code = GET_CODE (x);
5741
 
5742
  switch (code)
5743
    {
5744
    case SMAX:
5745
      return GE;
5746
    case SMIN:
5747
      return LE;
5748
    case UMIN:
5749
      return LEU;
5750
    case UMAX:
5751
      return GEU;
5752
    default:
5753
      gcc_unreachable ();
5754
    }
5755
}
5756
 
5757
/* Return 1 if memory locations are adjacent.  */
5758
int
5759
adjacent_mem_locations (rtx a, rtx b)
5760
{
5761
  /* We don't guarantee to preserve the order of these memory refs.  */
5762
  if (volatile_refs_p (a) || volatile_refs_p (b))
5763
    return 0;
5764
 
5765
  if ((GET_CODE (XEXP (a, 0)) == REG
5766
       || (GET_CODE (XEXP (a, 0)) == PLUS
5767
           && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5768
      && (GET_CODE (XEXP (b, 0)) == REG
5769
          || (GET_CODE (XEXP (b, 0)) == PLUS
5770
              && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5771
    {
5772
      HOST_WIDE_INT val0 = 0, val1 = 0;
5773
      rtx reg0, reg1;
5774
      int val_diff;
5775
 
5776
      if (GET_CODE (XEXP (a, 0)) == PLUS)
5777
        {
5778
          reg0 = XEXP (XEXP (a, 0), 0);
5779
          val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5780
        }
5781
      else
5782
        reg0 = XEXP (a, 0);
5783
 
5784
      if (GET_CODE (XEXP (b, 0)) == PLUS)
5785
        {
5786
          reg1 = XEXP (XEXP (b, 0), 0);
5787
          val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5788
        }
5789
      else
5790
        reg1 = XEXP (b, 0);
5791
 
5792
      /* Don't accept any offset that will require multiple
5793
         instructions to handle, since this would cause the
5794
         arith_adjacentmem pattern to output an overlong sequence.  */
5795
      if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5796
        return 0;
5797
 
5798
      /* Don't allow an eliminable register: register elimination can make
5799
         the offset too large.  */
5800
      if (arm_eliminable_register (reg0))
5801
        return 0;
5802
 
5803
      val_diff = val1 - val0;
5804
 
5805
      if (arm_ld_sched)
5806
        {
5807
          /* If the target has load delay slots, then there's no benefit
5808
             to using an ldm instruction unless the offset is zero and
5809
             we are optimizing for size.  */
5810
          return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5811
                  && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5812
                  && (val_diff == 4 || val_diff == -4));
5813
        }
5814
 
5815
      return ((REGNO (reg0) == REGNO (reg1))
5816
              && (val_diff == 4 || val_diff == -4));
5817
    }
5818
 
5819
  return 0;
5820
}
5821
 
5822
int
5823
load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5824
                        HOST_WIDE_INT *load_offset)
5825
{
5826
  int unsorted_regs[4];
5827
  HOST_WIDE_INT unsorted_offsets[4];
5828
  int order[4];
5829
  int base_reg = -1;
5830
  int i;
5831
 
5832
  /* Can only handle 2, 3, or 4 insns at present,
5833
     though could be easily extended if required.  */
5834
  gcc_assert (nops >= 2 && nops <= 4);
5835
 
5836
  /* Loop over the operands and check that the memory references are
5837
     suitable (i.e. immediate offsets from the same base register).  At
5838
     the same time, extract the target register, and the memory
5839
     offsets.  */
5840
  for (i = 0; i < nops; i++)
5841
    {
5842
      rtx reg;
5843
      rtx offset;
5844
 
5845
      /* Convert a subreg of a mem into the mem itself.  */
5846
      if (GET_CODE (operands[nops + i]) == SUBREG)
5847
        operands[nops + i] = alter_subreg (operands + (nops + i));
5848
 
5849
      gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5850
 
5851
      /* Don't reorder volatile memory references; it doesn't seem worth
5852
         looking for the case where the order is ok anyway.  */
5853
      if (MEM_VOLATILE_P (operands[nops + i]))
5854
        return 0;
5855
 
5856
      offset = const0_rtx;
5857
 
5858
      if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5859
           || (GET_CODE (reg) == SUBREG
5860
               && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5861
          || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5862
              && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5863
                   == REG)
5864
                  || (GET_CODE (reg) == SUBREG
5865
                      && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5866
              && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5867
                  == CONST_INT)))
5868
        {
5869
          if (i == 0)
5870
            {
5871
              base_reg = REGNO (reg);
5872
              unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5873
                                  ? REGNO (operands[i])
5874
                                  : REGNO (SUBREG_REG (operands[i])));
5875
              order[0] = 0;
5876
            }
5877
          else
5878
            {
5879
              if (base_reg != (int) REGNO (reg))
5880
                /* Not addressed from the same base register.  */
5881
                return 0;
5882
 
5883
              unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5884
                                  ? REGNO (operands[i])
5885
                                  : REGNO (SUBREG_REG (operands[i])));
5886
              if (unsorted_regs[i] < unsorted_regs[order[0]])
5887
                order[0] = i;
5888
            }
5889
 
5890
          /* If it isn't an integer register, or if it overwrites the
5891
             base register but isn't the last insn in the list, then
5892
             we can't do this.  */
5893
          if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5894
              || (i != nops - 1 && unsorted_regs[i] == base_reg))
5895
            return 0;
5896
 
5897
          unsorted_offsets[i] = INTVAL (offset);
5898
        }
5899
      else
5900
        /* Not a suitable memory address.  */
5901
        return 0;
5902
    }
5903
 
5904
  /* All the useful information has now been extracted from the
5905
     operands into unsorted_regs and unsorted_offsets; additionally,
5906
     order[0] has been set to the lowest numbered register in the
5907
     list.  Sort the registers into order, and check that the memory
5908
     offsets are ascending and adjacent.  */
5909
 
5910
  for (i = 1; i < nops; i++)
5911
    {
5912
      int j;
5913
 
5914
      order[i] = order[i - 1];
5915
      for (j = 0; j < nops; j++)
5916
        if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5917
            && (order[i] == order[i - 1]
5918
                || unsorted_regs[j] < unsorted_regs[order[i]]))
5919
          order[i] = j;
5920
 
5921
      /* Have we found a suitable register? if not, one must be used more
5922
         than once.  */
5923
      if (order[i] == order[i - 1])
5924
        return 0;
5925
 
5926
      /* Is the memory address adjacent and ascending? */
5927
      if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5928
        return 0;
5929
    }
5930
 
5931
  if (base)
5932
    {
5933
      *base = base_reg;
5934
 
5935
      for (i = 0; i < nops; i++)
5936
        regs[i] = unsorted_regs[order[i]];
5937
 
5938
      *load_offset = unsorted_offsets[order[0]];
5939
    }
5940
 
5941
  if (unsorted_offsets[order[0]] == 0)
5942
    return 1; /* ldmia */
5943
 
5944
  if (unsorted_offsets[order[0]] == 4)
5945
    return 2; /* ldmib */
5946
 
5947
  if (unsorted_offsets[order[nops - 1]] == 0)
5948
    return 3; /* ldmda */
5949
 
5950
  if (unsorted_offsets[order[nops - 1]] == -4)
5951
    return 4; /* ldmdb */
5952
 
5953
  /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5954
     if the offset isn't small enough.  The reason 2 ldrs are faster
5955
     is because these ARMs are able to do more than one cache access
5956
     in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5957
     whilst the ARM8 has a double bandwidth cache.  This means that
5958
     these cores can do both an instruction fetch and a data fetch in
5959
     a single cycle, so the trick of calculating the address into a
5960
     scratch register (one of the result regs) and then doing a load
5961
     multiple actually becomes slower (and no smaller in code size).
5962
     That is the transformation
5963
 
5964
        ldr     rd1, [rbase + offset]
5965
        ldr     rd2, [rbase + offset + 4]
5966
 
5967
     to
5968
 
5969
        add     rd1, rbase, offset
5970
        ldmia   rd1, {rd1, rd2}
5971
 
5972
     produces worse code -- '3 cycles + any stalls on rd2' instead of
5973
     '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5974
     access per cycle, the first sequence could never complete in less
5975
     than 6 cycles, whereas the ldm sequence would only take 5 and
5976
     would make better use of sequential accesses if not hitting the
5977
     cache.
5978
 
5979
     We cheat here and test 'arm_ld_sched' which we currently know to
5980
     only be true for the ARM8, ARM9 and StrongARM.  If this ever
5981
     changes, then the test below needs to be reworked.  */
5982
  if (nops == 2 && arm_ld_sched)
5983
    return 0;
5984
 
5985
  /* Can't do it without setting up the offset, only do this if it takes
5986
     no more than one insn.  */
5987
  return (const_ok_for_arm (unsorted_offsets[order[0]])
5988
          || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5989
}
5990
 
5991
const char *
5992
emit_ldm_seq (rtx *operands, int nops)
5993
{
5994
  int regs[4];
5995
  int base_reg;
5996
  HOST_WIDE_INT offset;
5997
  char buf[100];
5998
  int i;
5999
 
6000
  switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6001
    {
6002
    case 1:
6003
      strcpy (buf, "ldm%?ia\t");
6004
      break;
6005
 
6006
    case 2:
6007
      strcpy (buf, "ldm%?ib\t");
6008
      break;
6009
 
6010
    case 3:
6011
      strcpy (buf, "ldm%?da\t");
6012
      break;
6013
 
6014
    case 4:
6015
      strcpy (buf, "ldm%?db\t");
6016
      break;
6017
 
6018
    case 5:
6019
      if (offset >= 0)
6020
        sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6021
                 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6022
                 (long) offset);
6023
      else
6024
        sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6025
                 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6026
                 (long) -offset);
6027
      output_asm_insn (buf, operands);
6028
      base_reg = regs[0];
6029
      strcpy (buf, "ldm%?ia\t");
6030
      break;
6031
 
6032
    default:
6033
      gcc_unreachable ();
6034
    }
6035
 
6036
  sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6037
           reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6038
 
6039
  for (i = 1; i < nops; i++)
6040
    sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6041
             reg_names[regs[i]]);
6042
 
6043
  strcat (buf, "}\t%@ phole ldm");
6044
 
6045
  output_asm_insn (buf, operands);
6046
  return "";
6047
}
6048
 
6049
int
6050
store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6051
                         HOST_WIDE_INT * load_offset)
6052
{
6053
  int unsorted_regs[4];
6054
  HOST_WIDE_INT unsorted_offsets[4];
6055
  int order[4];
6056
  int base_reg = -1;
6057
  int i;
6058
 
6059
  /* Can only handle 2, 3, or 4 insns at present, though could be easily
6060
     extended if required.  */
6061
  gcc_assert (nops >= 2 && nops <= 4);
6062
 
6063
  /* Loop over the operands and check that the memory references are
6064
     suitable (i.e. immediate offsets from the same base register).  At
6065
     the same time, extract the target register, and the memory
6066
     offsets.  */
6067
  for (i = 0; i < nops; i++)
6068
    {
6069
      rtx reg;
6070
      rtx offset;
6071
 
6072
      /* Convert a subreg of a mem into the mem itself.  */
6073
      if (GET_CODE (operands[nops + i]) == SUBREG)
6074
        operands[nops + i] = alter_subreg (operands + (nops + i));
6075
 
6076
      gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6077
 
6078
      /* Don't reorder volatile memory references; it doesn't seem worth
6079
         looking for the case where the order is ok anyway.  */
6080
      if (MEM_VOLATILE_P (operands[nops + i]))
6081
        return 0;
6082
 
6083
      offset = const0_rtx;
6084
 
6085
      if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6086
           || (GET_CODE (reg) == SUBREG
6087
               && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6088
          || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6089
              && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6090
                   == REG)
6091
                  || (GET_CODE (reg) == SUBREG
6092
                      && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6093
              && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6094
                  == CONST_INT)))
6095
        {
6096
          if (i == 0)
6097
            {
6098
              base_reg = REGNO (reg);
6099
              unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6100
                                  ? REGNO (operands[i])
6101
                                  : REGNO (SUBREG_REG (operands[i])));
6102
              order[0] = 0;
6103
            }
6104
          else
6105
            {
6106
              if (base_reg != (int) REGNO (reg))
6107
                /* Not addressed from the same base register.  */
6108
                return 0;
6109
 
6110
              unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6111
                                  ? REGNO (operands[i])
6112
                                  : REGNO (SUBREG_REG (operands[i])));
6113
              if (unsorted_regs[i] < unsorted_regs[order[0]])
6114
                order[0] = i;
6115
            }
6116
 
6117
          /* If it isn't an integer register, then we can't do this.  */
6118
          if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6119
            return 0;
6120
 
6121
          unsorted_offsets[i] = INTVAL (offset);
6122
        }
6123
      else
6124
        /* Not a suitable memory address.  */
6125
        return 0;
6126
    }
6127
 
6128
  /* All the useful information has now been extracted from the
6129
     operands into unsorted_regs and unsorted_offsets; additionally,
6130
     order[0] has been set to the lowest numbered register in the
6131
     list.  Sort the registers into order, and check that the memory
6132
     offsets are ascending and adjacent.  */
6133
 
6134
  for (i = 1; i < nops; i++)
6135
    {
6136
      int j;
6137
 
6138
      order[i] = order[i - 1];
6139
      for (j = 0; j < nops; j++)
6140
        if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6141
            && (order[i] == order[i - 1]
6142
                || unsorted_regs[j] < unsorted_regs[order[i]]))
6143
          order[i] = j;
6144
 
6145
      /* Have we found a suitable register? if not, one must be used more
6146
         than once.  */
6147
      if (order[i] == order[i - 1])
6148
        return 0;
6149
 
6150
      /* Is the memory address adjacent and ascending? */
6151
      if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6152
        return 0;
6153
    }
6154
 
6155
  if (base)
6156
    {
6157
      *base = base_reg;
6158
 
6159
      for (i = 0; i < nops; i++)
6160
        regs[i] = unsorted_regs[order[i]];
6161
 
6162
      *load_offset = unsorted_offsets[order[0]];
6163
    }
6164
 
6165
  if (unsorted_offsets[order[0]] == 0)
6166
    return 1; /* stmia */
6167
 
6168
  if (unsorted_offsets[order[0]] == 4)
6169
    return 2; /* stmib */
6170
 
6171
  if (unsorted_offsets[order[nops - 1]] == 0)
6172
    return 3; /* stmda */
6173
 
6174
  if (unsorted_offsets[order[nops - 1]] == -4)
6175
    return 4; /* stmdb */
6176
 
6177
  return 0;
6178
}
6179
 
6180
const char *
6181
emit_stm_seq (rtx *operands, int nops)
6182
{
6183
  int regs[4];
6184
  int base_reg;
6185
  HOST_WIDE_INT offset;
6186
  char buf[100];
6187
  int i;
6188
 
6189
  switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6190
    {
6191
    case 1:
6192
      strcpy (buf, "stm%?ia\t");
6193
      break;
6194
 
6195
    case 2:
6196
      strcpy (buf, "stm%?ib\t");
6197
      break;
6198
 
6199
    case 3:
6200
      strcpy (buf, "stm%?da\t");
6201
      break;
6202
 
6203
    case 4:
6204
      strcpy (buf, "stm%?db\t");
6205
      break;
6206
 
6207
    default:
6208
      gcc_unreachable ();
6209
    }
6210
 
6211
  sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6212
           reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6213
 
6214
  for (i = 1; i < nops; i++)
6215
    sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6216
             reg_names[regs[i]]);
6217
 
6218
  strcat (buf, "}\t%@ phole stm");
6219
 
6220
  output_asm_insn (buf, operands);
6221
  return "";
6222
}
6223
 
6224
/* Routines for use in generating RTL.  */
6225
 
6226
rtx
6227
arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6228
                       int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6229
{
6230
  HOST_WIDE_INT offset = *offsetp;
6231
  int i = 0, j;
6232
  rtx result;
6233
  int sign = up ? 1 : -1;
6234
  rtx mem, addr;
6235
 
6236
  /* XScale has load-store double instructions, but they have stricter
6237
     alignment requirements than load-store multiple, so we cannot
6238
     use them.
6239
 
6240
     For XScale ldm requires 2 + NREGS cycles to complete and blocks
6241
     the pipeline until completion.
6242
 
6243
        NREGS           CYCLES
6244
          1               3
6245
          2               4
6246
          3               5
6247
          4               6
6248
 
6249
     An ldr instruction takes 1-3 cycles, but does not block the
6250
     pipeline.
6251
 
6252
        NREGS           CYCLES
6253
          1              1-3
6254
          2              2-6
6255
          3              3-9
6256
          4              4-12
6257
 
6258
     Best case ldr will always win.  However, the more ldr instructions
6259
     we issue, the less likely we are to be able to schedule them well.
6260
     Using ldr instructions also increases code size.
6261
 
6262
     As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6263
     for counts of 3 or 4 regs.  */
6264
  if (arm_tune_xscale && count <= 2 && ! optimize_size)
6265
    {
6266
      rtx seq;
6267
 
6268
      start_sequence ();
6269
 
6270
      for (i = 0; i < count; i++)
6271
        {
6272
          addr = plus_constant (from, i * 4 * sign);
6273
          mem = adjust_automodify_address (basemem, SImode, addr, offset);
6274
          emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6275
          offset += 4 * sign;
6276
        }
6277
 
6278
      if (write_back)
6279
        {
6280
          emit_move_insn (from, plus_constant (from, count * 4 * sign));
6281
          *offsetp = offset;
6282
        }
6283
 
6284
      seq = get_insns ();
6285
      end_sequence ();
6286
 
6287
      return seq;
6288
    }
6289
 
6290
  result = gen_rtx_PARALLEL (VOIDmode,
6291
                             rtvec_alloc (count + (write_back ? 1 : 0)));
6292
  if (write_back)
6293
    {
6294
      XVECEXP (result, 0, 0)
6295
        = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
6296
      i = 1;
6297
      count++;
6298
    }
6299
 
6300
  for (j = 0; i < count; i++, j++)
6301
    {
6302
      addr = plus_constant (from, j * 4 * sign);
6303
      mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6304
      XVECEXP (result, 0, i)
6305
        = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6306
      offset += 4 * sign;
6307
    }
6308
 
6309
  if (write_back)
6310
    *offsetp = offset;
6311
 
6312
  return result;
6313
}
6314
 
6315
rtx
6316
arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6317
                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6318
{
6319
  HOST_WIDE_INT offset = *offsetp;
6320
  int i = 0, j;
6321
  rtx result;
6322
  int sign = up ? 1 : -1;
6323
  rtx mem, addr;
6324
 
6325
  /* See arm_gen_load_multiple for discussion of
6326
     the pros/cons of ldm/stm usage for XScale.  */
6327
  if (arm_tune_xscale && count <= 2 && ! optimize_size)
6328
    {
6329
      rtx seq;
6330
 
6331
      start_sequence ();
6332
 
6333
      for (i = 0; i < count; i++)
6334
        {
6335
          addr = plus_constant (to, i * 4 * sign);
6336
          mem = adjust_automodify_address (basemem, SImode, addr, offset);
6337
          emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6338
          offset += 4 * sign;
6339
        }
6340
 
6341
      if (write_back)
6342
        {
6343
          emit_move_insn (to, plus_constant (to, count * 4 * sign));
6344
          *offsetp = offset;
6345
        }
6346
 
6347
      seq = get_insns ();
6348
      end_sequence ();
6349
 
6350
      return seq;
6351
    }
6352
 
6353
  result = gen_rtx_PARALLEL (VOIDmode,
6354
                             rtvec_alloc (count + (write_back ? 1 : 0)));
6355
  if (write_back)
6356
    {
6357
      XVECEXP (result, 0, 0)
6358
        = gen_rtx_SET (VOIDmode, to,
6359
                       plus_constant (to, count * 4 * sign));
6360
      i = 1;
6361
      count++;
6362
    }
6363
 
6364
  for (j = 0; i < count; i++, j++)
6365
    {
6366
      addr = plus_constant (to, j * 4 * sign);
6367
      mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6368
      XVECEXP (result, 0, i)
6369
        = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6370
      offset += 4 * sign;
6371
    }
6372
 
6373
  if (write_back)
6374
    *offsetp = offset;
6375
 
6376
  return result;
6377
}
6378
 
6379
int
6380
arm_gen_movmemqi (rtx *operands)
6381
{
6382
  HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6383
  HOST_WIDE_INT srcoffset, dstoffset;
6384
  int i;
6385
  rtx src, dst, srcbase, dstbase;
6386
  rtx part_bytes_reg = NULL;
6387
  rtx mem;
6388
 
6389
  if (GET_CODE (operands[2]) != CONST_INT
6390
      || GET_CODE (operands[3]) != CONST_INT
6391
      || INTVAL (operands[2]) > 64
6392
      || INTVAL (operands[3]) & 3)
6393
    return 0;
6394
 
6395
  dstbase = operands[0];
6396
  srcbase = operands[1];
6397
 
6398
  dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6399
  src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6400
 
6401
  in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6402
  out_words_to_go = INTVAL (operands[2]) / 4;
6403
  last_bytes = INTVAL (operands[2]) & 3;
6404
  dstoffset = srcoffset = 0;
6405
 
6406
  if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6407
    part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6408
 
6409
  for (i = 0; in_words_to_go >= 2; i+=4)
6410
    {
6411
      if (in_words_to_go > 4)
6412
        emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6413
                                          srcbase, &srcoffset));
6414
      else
6415
        emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6416
                                          FALSE, srcbase, &srcoffset));
6417
 
6418
      if (out_words_to_go)
6419
        {
6420
          if (out_words_to_go > 4)
6421
            emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6422
                                               dstbase, &dstoffset));
6423
          else if (out_words_to_go != 1)
6424
            emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6425
                                               dst, TRUE,
6426
                                               (last_bytes == 0
6427
                                                ? FALSE : TRUE),
6428
                                               dstbase, &dstoffset));
6429
          else
6430
            {
6431
              mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6432
              emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6433
              if (last_bytes != 0)
6434
                {
6435
                  emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6436
                  dstoffset += 4;
6437
                }
6438
            }
6439
        }
6440
 
6441
      in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6442
      out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6443
    }
6444
 
6445
  /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6446
  if (out_words_to_go)
6447
    {
6448
      rtx sreg;
6449
 
6450
      mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6451
      sreg = copy_to_reg (mem);
6452
 
6453
      mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6454
      emit_move_insn (mem, sreg);
6455
      in_words_to_go--;
6456
 
6457
      gcc_assert (!in_words_to_go);     /* Sanity check */
6458
    }
6459
 
6460
  if (in_words_to_go)
6461
    {
6462
      gcc_assert (in_words_to_go > 0);
6463
 
6464
      mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6465
      part_bytes_reg = copy_to_mode_reg (SImode, mem);
6466
    }
6467
 
6468
  gcc_assert (!last_bytes || part_bytes_reg);
6469
 
6470
  if (BYTES_BIG_ENDIAN && last_bytes)
6471
    {
6472
      rtx tmp = gen_reg_rtx (SImode);
6473
 
6474
      /* The bytes we want are in the top end of the word.  */
6475
      emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6476
                              GEN_INT (8 * (4 - last_bytes))));
6477
      part_bytes_reg = tmp;
6478
 
6479
      while (last_bytes)
6480
        {
6481
          mem = adjust_automodify_address (dstbase, QImode,
6482
                                           plus_constant (dst, last_bytes - 1),
6483
                                           dstoffset + last_bytes - 1);
6484
          emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6485
 
6486
          if (--last_bytes)
6487
            {
6488
              tmp = gen_reg_rtx (SImode);
6489
              emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6490
              part_bytes_reg = tmp;
6491
            }
6492
        }
6493
 
6494
    }
6495
  else
6496
    {
6497
      if (last_bytes > 1)
6498
        {
6499
          mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6500
          emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6501
          last_bytes -= 2;
6502
          if (last_bytes)
6503
            {
6504
              rtx tmp = gen_reg_rtx (SImode);
6505
              emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6506
              emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6507
              part_bytes_reg = tmp;
6508
              dstoffset += 2;
6509
            }
6510
        }
6511
 
6512
      if (last_bytes)
6513
        {
6514
          mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6515
          emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6516
        }
6517
    }
6518
 
6519
  return 1;
6520
}
6521
 
6522
/* Select a dominance comparison mode if possible for a test of the general
6523
   form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6524
   COND_OR == DOM_CC_X_AND_Y => (X && Y)
6525
   COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6526
   COND_OR == DOM_CC_X_OR_Y => (X || Y)
6527
   In all cases OP will be either EQ or NE, but we don't need to know which
6528
   here.  If we are unable to support a dominance comparison we return
6529
   CC mode.  This will then fail to match for the RTL expressions that
6530
   generate this call.  */
6531
enum machine_mode
6532
arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6533
{
6534
  enum rtx_code cond1, cond2;
6535
  int swapped = 0;
6536
 
6537
  /* Currently we will probably get the wrong result if the individual
6538
     comparisons are not simple.  This also ensures that it is safe to
6539
     reverse a comparison if necessary.  */
6540
  if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6541
       != CCmode)
6542
      || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6543
          != CCmode))
6544
    return CCmode;
6545
 
6546
  /* The if_then_else variant of this tests the second condition if the
6547
     first passes, but is true if the first fails.  Reverse the first
6548
     condition to get a true "inclusive-or" expression.  */
6549
  if (cond_or == DOM_CC_NX_OR_Y)
6550
    cond1 = reverse_condition (cond1);
6551
 
6552
  /* If the comparisons are not equal, and one doesn't dominate the other,
6553
     then we can't do this.  */
6554
  if (cond1 != cond2
6555
      && !comparison_dominates_p (cond1, cond2)
6556
      && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6557
    return CCmode;
6558
 
6559
  if (swapped)
6560
    {
6561
      enum rtx_code temp = cond1;
6562
      cond1 = cond2;
6563
      cond2 = temp;
6564
    }
6565
 
6566
  switch (cond1)
6567
    {
6568
    case EQ:
6569
      if (cond_or == DOM_CC_X_AND_Y)
6570
        return CC_DEQmode;
6571
 
6572
      switch (cond2)
6573
        {
6574
        case EQ: return CC_DEQmode;
6575
        case LE: return CC_DLEmode;
6576
        case LEU: return CC_DLEUmode;
6577
        case GE: return CC_DGEmode;
6578
        case GEU: return CC_DGEUmode;
6579
        default: gcc_unreachable ();
6580
        }
6581
 
6582
    case LT:
6583
      if (cond_or == DOM_CC_X_AND_Y)
6584
        return CC_DLTmode;
6585
 
6586
      switch (cond2)
6587
        {
6588
        case  LT:
6589
            return CC_DLTmode;
6590
        case LE:
6591
          return CC_DLEmode;
6592
        case NE:
6593
          return CC_DNEmode;
6594
        default:
6595
          gcc_unreachable ();
6596
        }
6597
 
6598
    case GT:
6599
      if (cond_or == DOM_CC_X_AND_Y)
6600
        return CC_DGTmode;
6601
 
6602
      switch (cond2)
6603
        {
6604
        case GT:
6605
          return CC_DGTmode;
6606
        case GE:
6607
          return CC_DGEmode;
6608
        case NE:
6609
          return CC_DNEmode;
6610
        default:
6611
          gcc_unreachable ();
6612
        }
6613
 
6614
    case LTU:
6615
      if (cond_or == DOM_CC_X_AND_Y)
6616
        return CC_DLTUmode;
6617
 
6618
      switch (cond2)
6619
        {
6620
        case LTU:
6621
          return CC_DLTUmode;
6622
        case LEU:
6623
          return CC_DLEUmode;
6624
        case NE:
6625
          return CC_DNEmode;
6626
        default:
6627
          gcc_unreachable ();
6628
        }
6629
 
6630
    case GTU:
6631
      if (cond_or == DOM_CC_X_AND_Y)
6632
        return CC_DGTUmode;
6633
 
6634
      switch (cond2)
6635
        {
6636
        case GTU:
6637
          return CC_DGTUmode;
6638
        case GEU:
6639
          return CC_DGEUmode;
6640
        case NE:
6641
          return CC_DNEmode;
6642
        default:
6643
          gcc_unreachable ();
6644
        }
6645
 
6646
    /* The remaining cases only occur when both comparisons are the
6647
       same.  */
6648
    case NE:
6649
      gcc_assert (cond1 == cond2);
6650
      return CC_DNEmode;
6651
 
6652
    case LE:
6653
      gcc_assert (cond1 == cond2);
6654
      return CC_DLEmode;
6655
 
6656
    case GE:
6657
      gcc_assert (cond1 == cond2);
6658
      return CC_DGEmode;
6659
 
6660
    case LEU:
6661
      gcc_assert (cond1 == cond2);
6662
      return CC_DLEUmode;
6663
 
6664
    case GEU:
6665
      gcc_assert (cond1 == cond2);
6666
      return CC_DGEUmode;
6667
 
6668
    default:
6669
      gcc_unreachable ();
6670
    }
6671
}
6672
 
6673
enum machine_mode
6674
arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6675
{
6676
  /* All floating point compares return CCFP if it is an equality
6677
     comparison, and CCFPE otherwise.  */
6678
  if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6679
    {
6680
      switch (op)
6681
        {
6682
        case EQ:
6683
        case NE:
6684
        case UNORDERED:
6685
        case ORDERED:
6686
        case UNLT:
6687
        case UNLE:
6688
        case UNGT:
6689
        case UNGE:
6690
        case UNEQ:
6691
        case LTGT:
6692
          return CCFPmode;
6693
 
6694
        case LT:
6695
        case LE:
6696
        case GT:
6697
        case GE:
6698
          if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6699
            return CCFPmode;
6700
          return CCFPEmode;
6701
 
6702
        default:
6703
          gcc_unreachable ();
6704
        }
6705
    }
6706
 
6707
  /* A compare with a shifted operand.  Because of canonicalization, the
6708
     comparison will have to be swapped when we emit the assembler.  */
6709
  if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6710
      && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6711
          || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6712
          || GET_CODE (x) == ROTATERT))
6713
    return CC_SWPmode;
6714
 
6715
  /* This operation is performed swapped, but since we only rely on the Z
6716
     flag we don't need an additional mode.  */
6717
  if (GET_MODE (y) == SImode && REG_P (y)
6718
      && GET_CODE (x) == NEG
6719
      && (op == EQ || op == NE))
6720
    return CC_Zmode;
6721
 
6722
  /* This is a special case that is used by combine to allow a
6723
     comparison of a shifted byte load to be split into a zero-extend
6724
     followed by a comparison of the shifted integer (only valid for
6725
     equalities and unsigned inequalities).  */
6726
  if (GET_MODE (x) == SImode
6727
      && GET_CODE (x) == ASHIFT
6728
      && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6729
      && GET_CODE (XEXP (x, 0)) == SUBREG
6730
      && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6731
      && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6732
      && (op == EQ || op == NE
6733
          || op == GEU || op == GTU || op == LTU || op == LEU)
6734
      && GET_CODE (y) == CONST_INT)
6735
    return CC_Zmode;
6736
 
6737
  /* A construct for a conditional compare, if the false arm contains
6738
     0, then both conditions must be true, otherwise either condition
6739
     must be true.  Not all conditions are possible, so CCmode is
6740
     returned if it can't be done.  */
6741
  if (GET_CODE (x) == IF_THEN_ELSE
6742
      && (XEXP (x, 2) == const0_rtx
6743
          || XEXP (x, 2) == const1_rtx)
6744
      && COMPARISON_P (XEXP (x, 0))
6745
      && COMPARISON_P (XEXP (x, 1)))
6746
    return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6747
                                         INTVAL (XEXP (x, 2)));
6748
 
6749
  /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6750
  if (GET_CODE (x) == AND
6751
      && COMPARISON_P (XEXP (x, 0))
6752
      && COMPARISON_P (XEXP (x, 1)))
6753
    return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6754
                                         DOM_CC_X_AND_Y);
6755
 
6756
  if (GET_CODE (x) == IOR
6757
      && COMPARISON_P (XEXP (x, 0))
6758
      && COMPARISON_P (XEXP (x, 1)))
6759
    return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6760
                                         DOM_CC_X_OR_Y);
6761
 
6762
  /* An operation (on Thumb) where we want to test for a single bit.
6763
     This is done by shifting that bit up into the top bit of a
6764
     scratch register; we can then branch on the sign bit.  */
6765
  if (TARGET_THUMB
6766
      && GET_MODE (x) == SImode
6767
      && (op == EQ || op == NE)
6768
      && GET_CODE (x) == ZERO_EXTRACT
6769
      && XEXP (x, 1) == const1_rtx)
6770
    return CC_Nmode;
6771
 
6772
  /* An operation that sets the condition codes as a side-effect, the
6773
     V flag is not set correctly, so we can only use comparisons where
6774
     this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6775
     instead.)  */
6776
  if (GET_MODE (x) == SImode
6777
      && y == const0_rtx
6778
      && (op == EQ || op == NE || op == LT || op == GE)
6779
      && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6780
          || GET_CODE (x) == AND || GET_CODE (x) == IOR
6781
          || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6782
          || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6783
          || GET_CODE (x) == LSHIFTRT
6784
          || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6785
          || GET_CODE (x) == ROTATERT
6786
          || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6787
    return CC_NOOVmode;
6788
 
6789
  if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6790
    return CC_Zmode;
6791
 
6792
  if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6793
      && GET_CODE (x) == PLUS
6794
      && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6795
    return CC_Cmode;
6796
 
6797
  return CCmode;
6798
}
6799
 
6800
/* X and Y are two things to compare using CODE.  Emit the compare insn and
6801
   return the rtx for register 0 in the proper mode.  FP means this is a
6802
   floating point compare: I don't think that it is needed on the arm.  */
6803
rtx
6804
arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6805
{
6806
  enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6807
  rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6808
 
6809
  emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
6810
 
6811
  return cc_reg;
6812
}
6813
 
6814
/* Generate a sequence of insns that will generate the correct return
6815
   address mask depending on the physical architecture that the program
6816
   is running on.  */
6817
rtx
6818
arm_gen_return_addr_mask (void)
6819
{
6820
  rtx reg = gen_reg_rtx (Pmode);
6821
 
6822
  emit_insn (gen_return_addr_mask (reg));
6823
  return reg;
6824
}
6825
 
6826
void
6827
arm_reload_in_hi (rtx *operands)
6828
{
6829
  rtx ref = operands[1];
6830
  rtx base, scratch;
6831
  HOST_WIDE_INT offset = 0;
6832
 
6833
  if (GET_CODE (ref) == SUBREG)
6834
    {
6835
      offset = SUBREG_BYTE (ref);
6836
      ref = SUBREG_REG (ref);
6837
    }
6838
 
6839
  if (GET_CODE (ref) == REG)
6840
    {
6841
      /* We have a pseudo which has been spilt onto the stack; there
6842
         are two cases here: the first where there is a simple
6843
         stack-slot replacement and a second where the stack-slot is
6844
         out of range, or is used as a subreg.  */
6845
      if (reg_equiv_mem[REGNO (ref)])
6846
        {
6847
          ref = reg_equiv_mem[REGNO (ref)];
6848
          base = find_replacement (&XEXP (ref, 0));
6849
        }
6850
      else
6851
        /* The slot is out of range, or was dressed up in a SUBREG.  */
6852
        base = reg_equiv_address[REGNO (ref)];
6853
    }
6854
  else
6855
    base = find_replacement (&XEXP (ref, 0));
6856
 
6857
  /* Handle the case where the address is too complex to be offset by 1.  */
6858
  if (GET_CODE (base) == MINUS
6859
      || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6860
    {
6861
      rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6862
 
6863
      emit_set_insn (base_plus, base);
6864
      base = base_plus;
6865
    }
6866
  else if (GET_CODE (base) == PLUS)
6867
    {
6868
      /* The addend must be CONST_INT, or we would have dealt with it above.  */
6869
      HOST_WIDE_INT hi, lo;
6870
 
6871
      offset += INTVAL (XEXP (base, 1));
6872
      base = XEXP (base, 0);
6873
 
6874
      /* Rework the address into a legal sequence of insns.  */
6875
      /* Valid range for lo is -4095 -> 4095 */
6876
      lo = (offset >= 0
6877
            ? (offset & 0xfff)
6878
            : -((-offset) & 0xfff));
6879
 
6880
      /* Corner case, if lo is the max offset then we would be out of range
6881
         once we have added the additional 1 below, so bump the msb into the
6882
         pre-loading insn(s).  */
6883
      if (lo == 4095)
6884
        lo &= 0x7ff;
6885
 
6886
      hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6887
             ^ (HOST_WIDE_INT) 0x80000000)
6888
            - (HOST_WIDE_INT) 0x80000000);
6889
 
6890
      gcc_assert (hi + lo == offset);
6891
 
6892
      if (hi != 0)
6893
        {
6894
          rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6895
 
6896
          /* Get the base address; addsi3 knows how to handle constants
6897
             that require more than one insn.  */
6898
          emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6899
          base = base_plus;
6900
          offset = lo;
6901
        }
6902
    }
6903
 
6904
  /* Operands[2] may overlap operands[0] (though it won't overlap
6905
     operands[1]), that's why we asked for a DImode reg -- so we can
6906
     use the bit that does not overlap.  */
6907
  if (REGNO (operands[2]) == REGNO (operands[0]))
6908
    scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6909
  else
6910
    scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6911
 
6912
  emit_insn (gen_zero_extendqisi2 (scratch,
6913
                                   gen_rtx_MEM (QImode,
6914
                                                plus_constant (base,
6915
                                                               offset))));
6916
  emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6917
                                   gen_rtx_MEM (QImode,
6918
                                                plus_constant (base,
6919
                                                               offset + 1))));
6920
  if (!BYTES_BIG_ENDIAN)
6921
    emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6922
                   gen_rtx_IOR (SImode,
6923
                                gen_rtx_ASHIFT
6924
                                (SImode,
6925
                                 gen_rtx_SUBREG (SImode, operands[0], 0),
6926
                                 GEN_INT (8)),
6927
                                scratch));
6928
  else
6929
    emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6930
                   gen_rtx_IOR (SImode,
6931
                                gen_rtx_ASHIFT (SImode, scratch,
6932
                                                GEN_INT (8)),
6933
                                gen_rtx_SUBREG (SImode, operands[0], 0)));
6934
}
6935
 
6936
/* Handle storing a half-word to memory during reload by synthesizing as two
6937
   byte stores.  Take care not to clobber the input values until after we
6938
   have moved them somewhere safe.  This code assumes that if the DImode
6939
   scratch in operands[2] overlaps either the input value or output address
6940
   in some way, then that value must die in this insn (we absolutely need
6941
   two scratch registers for some corner cases).  */
6942
void
6943
arm_reload_out_hi (rtx *operands)
6944
{
6945
  rtx ref = operands[0];
6946
  rtx outval = operands[1];
6947
  rtx base, scratch;
6948
  HOST_WIDE_INT offset = 0;
6949
 
6950
  if (GET_CODE (ref) == SUBREG)
6951
    {
6952
      offset = SUBREG_BYTE (ref);
6953
      ref = SUBREG_REG (ref);
6954
    }
6955
 
6956
  if (GET_CODE (ref) == REG)
6957
    {
6958
      /* We have a pseudo which has been spilt onto the stack; there
6959
         are two cases here: the first where there is a simple
6960
         stack-slot replacement and a second where the stack-slot is
6961
         out of range, or is used as a subreg.  */
6962
      if (reg_equiv_mem[REGNO (ref)])
6963
        {
6964
          ref = reg_equiv_mem[REGNO (ref)];
6965
          base = find_replacement (&XEXP (ref, 0));
6966
        }
6967
      else
6968
        /* The slot is out of range, or was dressed up in a SUBREG.  */
6969
        base = reg_equiv_address[REGNO (ref)];
6970
    }
6971
  else
6972
    base = find_replacement (&XEXP (ref, 0));
6973
 
6974
  scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6975
 
6976
  /* Handle the case where the address is too complex to be offset by 1.  */
6977
  if (GET_CODE (base) == MINUS
6978
      || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6979
    {
6980
      rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6981
 
6982
      /* Be careful not to destroy OUTVAL.  */
6983
      if (reg_overlap_mentioned_p (base_plus, outval))
6984
        {
6985
          /* Updating base_plus might destroy outval, see if we can
6986
             swap the scratch and base_plus.  */
6987
          if (!reg_overlap_mentioned_p (scratch, outval))
6988
            {
6989
              rtx tmp = scratch;
6990
              scratch = base_plus;
6991
              base_plus = tmp;
6992
            }
6993
          else
6994
            {
6995
              rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6996
 
6997
              /* Be conservative and copy OUTVAL into the scratch now,
6998
                 this should only be necessary if outval is a subreg
6999
                 of something larger than a word.  */
7000
              /* XXX Might this clobber base?  I can't see how it can,
7001
                 since scratch is known to overlap with OUTVAL, and
7002
                 must be wider than a word.  */
7003
              emit_insn (gen_movhi (scratch_hi, outval));
7004
              outval = scratch_hi;
7005
            }
7006
        }
7007
 
7008
      emit_set_insn (base_plus, base);
7009
      base = base_plus;
7010
    }
7011
  else if (GET_CODE (base) == PLUS)
7012
    {
7013
      /* The addend must be CONST_INT, or we would have dealt with it above.  */
7014
      HOST_WIDE_INT hi, lo;
7015
 
7016
      offset += INTVAL (XEXP (base, 1));
7017
      base = XEXP (base, 0);
7018
 
7019
      /* Rework the address into a legal sequence of insns.  */
7020
      /* Valid range for lo is -4095 -> 4095 */
7021
      lo = (offset >= 0
7022
            ? (offset & 0xfff)
7023
            : -((-offset) & 0xfff));
7024
 
7025
      /* Corner case, if lo is the max offset then we would be out of range
7026
         once we have added the additional 1 below, so bump the msb into the
7027
         pre-loading insn(s).  */
7028
      if (lo == 4095)
7029
        lo &= 0x7ff;
7030
 
7031
      hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7032
             ^ (HOST_WIDE_INT) 0x80000000)
7033
            - (HOST_WIDE_INT) 0x80000000);
7034
 
7035
      gcc_assert (hi + lo == offset);
7036
 
7037
      if (hi != 0)
7038
        {
7039
          rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7040
 
7041
          /* Be careful not to destroy OUTVAL.  */
7042
          if (reg_overlap_mentioned_p (base_plus, outval))
7043
            {
7044
              /* Updating base_plus might destroy outval, see if we
7045
                 can swap the scratch and base_plus.  */
7046
              if (!reg_overlap_mentioned_p (scratch, outval))
7047
                {
7048
                  rtx tmp = scratch;
7049
                  scratch = base_plus;
7050
                  base_plus = tmp;
7051
                }
7052
              else
7053
                {
7054
                  rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7055
 
7056
                  /* Be conservative and copy outval into scratch now,
7057
                     this should only be necessary if outval is a
7058
                     subreg of something larger than a word.  */
7059
                  /* XXX Might this clobber base?  I can't see how it
7060
                     can, since scratch is known to overlap with
7061
                     outval.  */
7062
                  emit_insn (gen_movhi (scratch_hi, outval));
7063
                  outval = scratch_hi;
7064
                }
7065
            }
7066
 
7067
          /* Get the base address; addsi3 knows how to handle constants
7068
             that require more than one insn.  */
7069
          emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7070
          base = base_plus;
7071
          offset = lo;
7072
        }
7073
    }
7074
 
7075
  if (BYTES_BIG_ENDIAN)
7076
    {
7077
      emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7078
                                         plus_constant (base, offset + 1)),
7079
                            gen_lowpart (QImode, outval)));
7080
      emit_insn (gen_lshrsi3 (scratch,
7081
                              gen_rtx_SUBREG (SImode, outval, 0),
7082
                              GEN_INT (8)));
7083
      emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7084
                            gen_lowpart (QImode, scratch)));
7085
    }
7086
  else
7087
    {
7088
      emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7089
                            gen_lowpart (QImode, outval)));
7090
      emit_insn (gen_lshrsi3 (scratch,
7091
                              gen_rtx_SUBREG (SImode, outval, 0),
7092
                              GEN_INT (8)));
7093
      emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7094
                                         plus_constant (base, offset + 1)),
7095
                            gen_lowpart (QImode, scratch)));
7096
    }
7097
}
7098
 
7099
/* Return true if a type must be passed in memory. For AAPCS, small aggregates
7100
   (padded to the size of a word) should be passed in a register.  */
7101
 
7102
static bool
7103
arm_must_pass_in_stack (enum machine_mode mode, tree type)
7104
{
7105
  if (TARGET_AAPCS_BASED)
7106
    return must_pass_in_stack_var_size (mode, type);
7107
  else
7108
    return must_pass_in_stack_var_size_or_pad (mode, type);
7109
}
7110
 
7111
 
7112
/* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7113
   Return true if an argument passed on the stack should be padded upwards,
7114
   i.e. if the least-significant byte has useful data.
7115
   For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
7116
   aggregate types are placed in the lowest memory address.  */
7117
 
7118
bool
7119
arm_pad_arg_upward (enum machine_mode mode, tree type)
7120
{
7121
  if (!TARGET_AAPCS_BASED)
7122
    return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7123
 
7124
  if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7125
    return false;
7126
 
7127
  return true;
7128
}
7129
 
7130
 
7131
/* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7132
   For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7133
   byte of the register has useful data, and return the opposite if the
7134
   most significant byte does.
7135
   For AAPCS, small aggregates and small complex types are always padded
7136
   upwards.  */
7137
 
7138
bool
7139
arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7140
                    tree type, int first ATTRIBUTE_UNUSED)
7141
{
7142
  if (TARGET_AAPCS_BASED
7143
      && BYTES_BIG_ENDIAN
7144
      && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7145
      && int_size_in_bytes (type) <= 4)
7146
    return true;
7147
 
7148
  /* Otherwise, use default padding.  */
7149
  return !BYTES_BIG_ENDIAN;
7150
}
7151
 
7152
 
7153
/* Print a symbolic form of X to the debug file, F.  */
7154
static void
7155
arm_print_value (FILE *f, rtx x)
7156
{
7157
  switch (GET_CODE (x))
7158
    {
7159
    case CONST_INT:
7160
      fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7161
      return;
7162
 
7163
    case CONST_DOUBLE:
7164
      fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7165
      return;
7166
 
7167
    case CONST_VECTOR:
7168
      {
7169
        int i;
7170
 
7171
        fprintf (f, "<");
7172
        for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7173
          {
7174
            fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7175
            if (i < (CONST_VECTOR_NUNITS (x) - 1))
7176
              fputc (',', f);
7177
          }
7178
        fprintf (f, ">");
7179
      }
7180
      return;
7181
 
7182
    case CONST_STRING:
7183
      fprintf (f, "\"%s\"", XSTR (x, 0));
7184
      return;
7185
 
7186
    case SYMBOL_REF:
7187
      fprintf (f, "`%s'", XSTR (x, 0));
7188
      return;
7189
 
7190
    case LABEL_REF:
7191
      fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7192
      return;
7193
 
7194
    case CONST:
7195
      arm_print_value (f, XEXP (x, 0));
7196
      return;
7197
 
7198
    case PLUS:
7199
      arm_print_value (f, XEXP (x, 0));
7200
      fprintf (f, "+");
7201
      arm_print_value (f, XEXP (x, 1));
7202
      return;
7203
 
7204
    case PC:
7205
      fprintf (f, "pc");
7206
      return;
7207
 
7208
    default:
7209
      fprintf (f, "????");
7210
      return;
7211
    }
7212
}
7213
 
7214
/* Routines for manipulation of the constant pool.  */
7215
 
7216
/* Arm instructions cannot load a large constant directly into a
7217
   register; they have to come from a pc relative load.  The constant
7218
   must therefore be placed in the addressable range of the pc
7219
   relative load.  Depending on the precise pc relative load
7220
   instruction the range is somewhere between 256 bytes and 4k.  This
7221
   means that we often have to dump a constant inside a function, and
7222
   generate code to branch around it.
7223
 
7224
   It is important to minimize this, since the branches will slow
7225
   things down and make the code larger.
7226
 
7227
   Normally we can hide the table after an existing unconditional
7228
   branch so that there is no interruption of the flow, but in the
7229
   worst case the code looks like this:
7230
 
7231
        ldr     rn, L1
7232
        ...
7233
        b       L2
7234
        align
7235
        L1:     .long value
7236
        L2:
7237
        ...
7238
 
7239
        ldr     rn, L3
7240
        ...
7241
        b       L4
7242
        align
7243
        L3:     .long value
7244
        L4:
7245
        ...
7246
 
7247
   We fix this by performing a scan after scheduling, which notices
7248
   which instructions need to have their operands fetched from the
7249
   constant table and builds the table.
7250
 
7251
   The algorithm starts by building a table of all the constants that
7252
   need fixing up and all the natural barriers in the function (places
7253
   where a constant table can be dropped without breaking the flow).
7254
   For each fixup we note how far the pc-relative replacement will be
7255
   able to reach and the offset of the instruction into the function.
7256
 
7257
   Having built the table we then group the fixes together to form
7258
   tables that are as large as possible (subject to addressing
7259
   constraints) and emit each table of constants after the last
7260
   barrier that is within range of all the instructions in the group.
7261
   If a group does not contain a barrier, then we forcibly create one
7262
   by inserting a jump instruction into the flow.  Once the table has
7263
   been inserted, the insns are then modified to reference the
7264
   relevant entry in the pool.
7265
 
7266
   Possible enhancements to the algorithm (not implemented) are:
7267
 
7268
   1) For some processors and object formats, there may be benefit in
7269
   aligning the pools to the start of cache lines; this alignment
7270
   would need to be taken into account when calculating addressability
7271
   of a pool.  */
7272
 
7273
/* These typedefs are located at the start of this file, so that
7274
   they can be used in the prototypes there.  This comment is to
7275
   remind readers of that fact so that the following structures
7276
   can be understood more easily.
7277
 
7278
     typedef struct minipool_node    Mnode;
7279
     typedef struct minipool_fixup   Mfix;  */
7280
 
7281
struct minipool_node
7282
{
7283
  /* Doubly linked chain of entries.  */
7284
  Mnode * next;
7285
  Mnode * prev;
7286
  /* The maximum offset into the code that this entry can be placed.  While
7287
     pushing fixes for forward references, all entries are sorted in order
7288
     of increasing max_address.  */
7289
  HOST_WIDE_INT max_address;
7290
  /* Similarly for an entry inserted for a backwards ref.  */
7291
  HOST_WIDE_INT min_address;
7292
  /* The number of fixes referencing this entry.  This can become zero
7293
     if we "unpush" an entry.  In this case we ignore the entry when we
7294
     come to emit the code.  */
7295
  int refcount;
7296
  /* The offset from the start of the minipool.  */
7297
  HOST_WIDE_INT offset;
7298
  /* The value in table.  */
7299
  rtx value;
7300
  /* The mode of value.  */
7301
  enum machine_mode mode;
7302
  /* The size of the value.  With iWMMXt enabled
7303
     sizes > 4 also imply an alignment of 8-bytes.  */
7304
  int fix_size;
7305
};
7306
 
7307
struct minipool_fixup
7308
{
7309
  Mfix *            next;
7310
  rtx               insn;
7311
  HOST_WIDE_INT     address;
7312
  rtx *             loc;
7313
  enum machine_mode mode;
7314
  int               fix_size;
7315
  rtx               value;
7316
  Mnode *           minipool;
7317
  HOST_WIDE_INT     forwards;
7318
  HOST_WIDE_INT     backwards;
7319
};
7320
 
7321
/* Fixes less than a word need padding out to a word boundary.  */
7322
#define MINIPOOL_FIX_SIZE(mode) \
7323
  (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7324
 
7325
static Mnode *  minipool_vector_head;
7326
static Mnode *  minipool_vector_tail;
7327
static rtx      minipool_vector_label;
7328
static int      minipool_pad;
7329
 
7330
/* The linked list of all minipool fixes required for this function.  */
7331
Mfix *          minipool_fix_head;
7332
Mfix *          minipool_fix_tail;
7333
/* The fix entry for the current minipool, once it has been placed.  */
7334
Mfix *          minipool_barrier;
7335
 
7336
/* Determines if INSN is the start of a jump table.  Returns the end
7337
   of the TABLE or NULL_RTX.  */
7338
static rtx
7339
is_jump_table (rtx insn)
7340
{
7341
  rtx table;
7342
 
7343
  if (GET_CODE (insn) == JUMP_INSN
7344
      && JUMP_LABEL (insn) != NULL
7345
      && ((table = next_real_insn (JUMP_LABEL (insn)))
7346
          == next_real_insn (insn))
7347
      && table != NULL
7348
      && GET_CODE (table) == JUMP_INSN
7349
      && (GET_CODE (PATTERN (table)) == ADDR_VEC
7350
          || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7351
    return table;
7352
 
7353
  return NULL_RTX;
7354
}
7355
 
7356
#ifndef JUMP_TABLES_IN_TEXT_SECTION
7357
#define JUMP_TABLES_IN_TEXT_SECTION 0
7358
#endif
7359
 
7360
static HOST_WIDE_INT
7361
get_jump_table_size (rtx insn)
7362
{
7363
  /* ADDR_VECs only take room if read-only data does into the text
7364
     section.  */
7365
  if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
7366
    {
7367
      rtx body = PATTERN (insn);
7368
      int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7369
 
7370
      return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
7371
    }
7372
 
7373
  return 0;
7374
}
7375
 
7376
/* Move a minipool fix MP from its current location to before MAX_MP.
7377
   If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7378
   constraints may need updating.  */
7379
static Mnode *
7380
move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7381
                               HOST_WIDE_INT max_address)
7382
{
7383
  /* The code below assumes these are different.  */
7384
  gcc_assert (mp != max_mp);
7385
 
7386
  if (max_mp == NULL)
7387
    {
7388
      if (max_address < mp->max_address)
7389
        mp->max_address = max_address;
7390
    }
7391
  else
7392
    {
7393
      if (max_address > max_mp->max_address - mp->fix_size)
7394
        mp->max_address = max_mp->max_address - mp->fix_size;
7395
      else
7396
        mp->max_address = max_address;
7397
 
7398
      /* Unlink MP from its current position.  Since max_mp is non-null,
7399
       mp->prev must be non-null.  */
7400
      mp->prev->next = mp->next;
7401
      if (mp->next != NULL)
7402
        mp->next->prev = mp->prev;
7403
      else
7404
        minipool_vector_tail = mp->prev;
7405
 
7406
      /* Re-insert it before MAX_MP.  */
7407
      mp->next = max_mp;
7408
      mp->prev = max_mp->prev;
7409
      max_mp->prev = mp;
7410
 
7411
      if (mp->prev != NULL)
7412
        mp->prev->next = mp;
7413
      else
7414
        minipool_vector_head = mp;
7415
    }
7416
 
7417
  /* Save the new entry.  */
7418
  max_mp = mp;
7419
 
7420
  /* Scan over the preceding entries and adjust their addresses as
7421
     required.  */
7422
  while (mp->prev != NULL
7423
         && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7424
    {
7425
      mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7426
      mp = mp->prev;
7427
    }
7428
 
7429
  return max_mp;
7430
}
7431
 
7432
/* Add a constant to the minipool for a forward reference.  Returns the
7433
   node added or NULL if the constant will not fit in this pool.  */
7434
static Mnode *
7435
add_minipool_forward_ref (Mfix *fix)
7436
{
7437
  /* If set, max_mp is the first pool_entry that has a lower
7438
     constraint than the one we are trying to add.  */
7439
  Mnode *       max_mp = NULL;
7440
  HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
7441
  Mnode *       mp;
7442
 
7443
  /* If the minipool starts before the end of FIX->INSN then this FIX
7444
     can not be placed into the current pool.  Furthermore, adding the
7445
     new constant pool entry may cause the pool to start FIX_SIZE bytes
7446
     earlier.  */
7447
  if (minipool_vector_head &&
7448
      (fix->address + get_attr_length (fix->insn)
7449
       >= minipool_vector_head->max_address - fix->fix_size))
7450
    return NULL;
7451
 
7452
  /* Scan the pool to see if a constant with the same value has
7453
     already been added.  While we are doing this, also note the
7454
     location where we must insert the constant if it doesn't already
7455
     exist.  */
7456
  for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7457
    {
7458
      if (GET_CODE (fix->value) == GET_CODE (mp->value)
7459
          && fix->mode == mp->mode
7460
          && (GET_CODE (fix->value) != CODE_LABEL
7461
              || (CODE_LABEL_NUMBER (fix->value)
7462
                  == CODE_LABEL_NUMBER (mp->value)))
7463
          && rtx_equal_p (fix->value, mp->value))
7464
        {
7465
          /* More than one fix references this entry.  */
7466
          mp->refcount++;
7467
          return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7468
        }
7469
 
7470
      /* Note the insertion point if necessary.  */
7471
      if (max_mp == NULL
7472
          && mp->max_address > max_address)
7473
        max_mp = mp;
7474
 
7475
      /* If we are inserting an 8-bytes aligned quantity and
7476
         we have not already found an insertion point, then
7477
         make sure that all such 8-byte aligned quantities are
7478
         placed at the start of the pool.  */
7479
      if (ARM_DOUBLEWORD_ALIGN
7480
          && max_mp == NULL
7481
          && fix->fix_size == 8
7482
          && mp->fix_size != 8)
7483
        {
7484
          max_mp = mp;
7485
          max_address = mp->max_address;
7486
        }
7487
    }
7488
 
7489
  /* The value is not currently in the minipool, so we need to create
7490
     a new entry for it.  If MAX_MP is NULL, the entry will be put on
7491
     the end of the list since the placement is less constrained than
7492
     any existing entry.  Otherwise, we insert the new fix before
7493
     MAX_MP and, if necessary, adjust the constraints on the other
7494
     entries.  */
7495
  mp = XNEW (Mnode);
7496
  mp->fix_size = fix->fix_size;
7497
  mp->mode = fix->mode;
7498
  mp->value = fix->value;
7499
  mp->refcount = 1;
7500
  /* Not yet required for a backwards ref.  */
7501
  mp->min_address = -65536;
7502
 
7503
  if (max_mp == NULL)
7504
    {
7505
      mp->max_address = max_address;
7506
      mp->next = NULL;
7507
      mp->prev = minipool_vector_tail;
7508
 
7509
      if (mp->prev == NULL)
7510
        {
7511
          minipool_vector_head = mp;
7512
          minipool_vector_label = gen_label_rtx ();
7513
        }
7514
      else
7515
        mp->prev->next = mp;
7516
 
7517
      minipool_vector_tail = mp;
7518
    }
7519
  else
7520
    {
7521
      if (max_address > max_mp->max_address - mp->fix_size)
7522
        mp->max_address = max_mp->max_address - mp->fix_size;
7523
      else
7524
        mp->max_address = max_address;
7525
 
7526
      mp->next = max_mp;
7527
      mp->prev = max_mp->prev;
7528
      max_mp->prev = mp;
7529
      if (mp->prev != NULL)
7530
        mp->prev->next = mp;
7531
      else
7532
        minipool_vector_head = mp;
7533
    }
7534
 
7535
  /* Save the new entry.  */
7536
  max_mp = mp;
7537
 
7538
  /* Scan over the preceding entries and adjust their addresses as
7539
     required.  */
7540
  while (mp->prev != NULL
7541
         && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7542
    {
7543
      mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7544
      mp = mp->prev;
7545
    }
7546
 
7547
  return max_mp;
7548
}
7549
 
7550
static Mnode *
7551
move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7552
                                HOST_WIDE_INT  min_address)
7553
{
7554
  HOST_WIDE_INT offset;
7555
 
7556
  /* The code below assumes these are different.  */
7557
  gcc_assert (mp != min_mp);
7558
 
7559
  if (min_mp == NULL)
7560
    {
7561
      if (min_address > mp->min_address)
7562
        mp->min_address = min_address;
7563
    }
7564
  else
7565
    {
7566
      /* We will adjust this below if it is too loose.  */
7567
      mp->min_address = min_address;
7568
 
7569
      /* Unlink MP from its current position.  Since min_mp is non-null,
7570
         mp->next must be non-null.  */
7571
      mp->next->prev = mp->prev;
7572
      if (mp->prev != NULL)
7573
        mp->prev->next = mp->next;
7574
      else
7575
        minipool_vector_head = mp->next;
7576
 
7577
      /* Reinsert it after MIN_MP.  */
7578
      mp->prev = min_mp;
7579
      mp->next = min_mp->next;
7580
      min_mp->next = mp;
7581
      if (mp->next != NULL)
7582
        mp->next->prev = mp;
7583
      else
7584
        minipool_vector_tail = mp;
7585
    }
7586
 
7587
  min_mp = mp;
7588
 
7589
  offset = 0;
7590
  for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7591
    {
7592
      mp->offset = offset;
7593
      if (mp->refcount > 0)
7594
        offset += mp->fix_size;
7595
 
7596
      if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7597
        mp->next->min_address = mp->min_address + mp->fix_size;
7598
    }
7599
 
7600
  return min_mp;
7601
}
7602
 
7603
/* Add a constant to the minipool for a backward reference.  Returns the
7604
   node added or NULL if the constant will not fit in this pool.
7605
 
7606
   Note that the code for insertion for a backwards reference can be
7607
   somewhat confusing because the calculated offsets for each fix do
7608
   not take into account the size of the pool (which is still under
7609
   construction.  */
7610
static Mnode *
7611
add_minipool_backward_ref (Mfix *fix)
7612
{
7613
  /* If set, min_mp is the last pool_entry that has a lower constraint
7614
     than the one we are trying to add.  */
7615
  Mnode *min_mp = NULL;
7616
  /* This can be negative, since it is only a constraint.  */
7617
  HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7618
  Mnode *mp;
7619
 
7620
  /* If we can't reach the current pool from this insn, or if we can't
7621
     insert this entry at the end of the pool without pushing other
7622
     fixes out of range, then we don't try.  This ensures that we
7623
     can't fail later on.  */
7624
  if (min_address >= minipool_barrier->address
7625
      || (minipool_vector_tail->min_address + fix->fix_size
7626
          >= minipool_barrier->address))
7627
    return NULL;
7628
 
7629
  /* Scan the pool to see if a constant with the same value has
7630
     already been added.  While we are doing this, also note the
7631
     location where we must insert the constant if it doesn't already
7632
     exist.  */
7633
  for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7634
    {
7635
      if (GET_CODE (fix->value) == GET_CODE (mp->value)
7636
          && fix->mode == mp->mode
7637
          && (GET_CODE (fix->value) != CODE_LABEL
7638
              || (CODE_LABEL_NUMBER (fix->value)
7639
                  == CODE_LABEL_NUMBER (mp->value)))
7640
          && rtx_equal_p (fix->value, mp->value)
7641
          /* Check that there is enough slack to move this entry to the
7642
             end of the table (this is conservative).  */
7643
          && (mp->max_address
7644
              > (minipool_barrier->address
7645
                 + minipool_vector_tail->offset
7646
                 + minipool_vector_tail->fix_size)))
7647
        {
7648
          mp->refcount++;
7649
          return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7650
        }
7651
 
7652
      if (min_mp != NULL)
7653
        mp->min_address += fix->fix_size;
7654
      else
7655
        {
7656
          /* Note the insertion point if necessary.  */
7657
          if (mp->min_address < min_address)
7658
            {
7659
              /* For now, we do not allow the insertion of 8-byte alignment
7660
                 requiring nodes anywhere but at the start of the pool.  */
7661
              if (ARM_DOUBLEWORD_ALIGN
7662
                  && fix->fix_size == 8 && mp->fix_size != 8)
7663
                return NULL;
7664
              else
7665
                min_mp = mp;
7666
            }
7667
          else if (mp->max_address
7668
                   < minipool_barrier->address + mp->offset + fix->fix_size)
7669
            {
7670
              /* Inserting before this entry would push the fix beyond
7671
                 its maximum address (which can happen if we have
7672
                 re-located a forwards fix); force the new fix to come
7673
                 after it.  */
7674
              min_mp = mp;
7675
              min_address = mp->min_address + fix->fix_size;
7676
            }
7677
          /* If we are inserting an 8-bytes aligned quantity and
7678
             we have not already found an insertion point, then
7679
             make sure that all such 8-byte aligned quantities are
7680
             placed at the start of the pool.  */
7681
          else if (ARM_DOUBLEWORD_ALIGN
7682
                   && min_mp == NULL
7683
                   && fix->fix_size == 8
7684
                   && mp->fix_size < 8)
7685
            {
7686
              min_mp = mp;
7687
              min_address = mp->min_address + fix->fix_size;
7688
            }
7689
        }
7690
    }
7691
 
7692
  /* We need to create a new entry.  */
7693
  mp = XNEW (Mnode);
7694
  mp->fix_size = fix->fix_size;
7695
  mp->mode = fix->mode;
7696
  mp->value = fix->value;
7697
  mp->refcount = 1;
7698
  mp->max_address = minipool_barrier->address + 65536;
7699
 
7700
  mp->min_address = min_address;
7701
 
7702
  if (min_mp == NULL)
7703
    {
7704
      mp->prev = NULL;
7705
      mp->next = minipool_vector_head;
7706
 
7707
      if (mp->next == NULL)
7708
        {
7709
          minipool_vector_tail = mp;
7710
          minipool_vector_label = gen_label_rtx ();
7711
        }
7712
      else
7713
        mp->next->prev = mp;
7714
 
7715
      minipool_vector_head = mp;
7716
    }
7717
  else
7718
    {
7719
      mp->next = min_mp->next;
7720
      mp->prev = min_mp;
7721
      min_mp->next = mp;
7722
 
7723
      if (mp->next != NULL)
7724
        mp->next->prev = mp;
7725
      else
7726
        minipool_vector_tail = mp;
7727
    }
7728
 
7729
  /* Save the new entry.  */
7730
  min_mp = mp;
7731
 
7732
  if (mp->prev)
7733
    mp = mp->prev;
7734
  else
7735
    mp->offset = 0;
7736
 
7737
  /* Scan over the following entries and adjust their offsets.  */
7738
  while (mp->next != NULL)
7739
    {
7740
      if (mp->next->min_address < mp->min_address + mp->fix_size)
7741
        mp->next->min_address = mp->min_address + mp->fix_size;
7742
 
7743
      if (mp->refcount)
7744
        mp->next->offset = mp->offset + mp->fix_size;
7745
      else
7746
        mp->next->offset = mp->offset;
7747
 
7748
      mp = mp->next;
7749
    }
7750
 
7751
  return min_mp;
7752
}
7753
 
7754
static void
7755
assign_minipool_offsets (Mfix *barrier)
7756
{
7757
  HOST_WIDE_INT offset = 0;
7758
  Mnode *mp;
7759
 
7760
  minipool_barrier = barrier;
7761
 
7762
  for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7763
    {
7764
      mp->offset = offset;
7765
 
7766
      if (mp->refcount > 0)
7767
        offset += mp->fix_size;
7768
    }
7769
}
7770
 
7771
/* Output the literal table */
7772
static void
7773
dump_minipool (rtx scan)
7774
{
7775
  Mnode * mp;
7776
  Mnode * nmp;
7777
  int align64 = 0;
7778
 
7779
  if (ARM_DOUBLEWORD_ALIGN)
7780
    for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7781
      if (mp->refcount > 0 && mp->fix_size == 8)
7782
        {
7783
          align64 = 1;
7784
          break;
7785
        }
7786
 
7787
  if (dump_file)
7788
    fprintf (dump_file,
7789
             ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7790
             INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7791
 
7792
  scan = emit_label_after (gen_label_rtx (), scan);
7793
  scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7794
  scan = emit_label_after (minipool_vector_label, scan);
7795
 
7796
  for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7797
    {
7798
      if (mp->refcount > 0)
7799
        {
7800
          if (dump_file)
7801
            {
7802
              fprintf (dump_file,
7803
                       ";;  Offset %u, min %ld, max %ld ",
7804
                       (unsigned) mp->offset, (unsigned long) mp->min_address,
7805
                       (unsigned long) mp->max_address);
7806
              arm_print_value (dump_file, mp->value);
7807
              fputc ('\n', dump_file);
7808
            }
7809
 
7810
          switch (mp->fix_size)
7811
            {
7812
#ifdef HAVE_consttable_1
7813
            case 1:
7814
              scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7815
              break;
7816
 
7817
#endif
7818
#ifdef HAVE_consttable_2
7819
            case 2:
7820
              scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7821
              break;
7822
 
7823
#endif
7824
#ifdef HAVE_consttable_4
7825
            case 4:
7826
              scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7827
              break;
7828
 
7829
#endif
7830
#ifdef HAVE_consttable_8
7831
            case 8:
7832
              scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7833
              break;
7834
 
7835
#endif
7836
            default:
7837
              gcc_unreachable ();
7838
            }
7839
        }
7840
 
7841
      nmp = mp->next;
7842
      free (mp);
7843
    }
7844
 
7845
  minipool_vector_head = minipool_vector_tail = NULL;
7846
  scan = emit_insn_after (gen_consttable_end (), scan);
7847
  scan = emit_barrier_after (scan);
7848
}
7849
 
7850
/* Return the cost of forcibly inserting a barrier after INSN.  */
7851
static int
7852
arm_barrier_cost (rtx insn)
7853
{
7854
  /* Basing the location of the pool on the loop depth is preferable,
7855
     but at the moment, the basic block information seems to be
7856
     corrupt by this stage of the compilation.  */
7857
  int base_cost = 50;
7858
  rtx next = next_nonnote_insn (insn);
7859
 
7860
  if (next != NULL && GET_CODE (next) == CODE_LABEL)
7861
    base_cost -= 20;
7862
 
7863
  switch (GET_CODE (insn))
7864
    {
7865
    case CODE_LABEL:
7866
      /* It will always be better to place the table before the label, rather
7867
         than after it.  */
7868
      return 50;
7869
 
7870
    case INSN:
7871
    case CALL_INSN:
7872
      return base_cost;
7873
 
7874
    case JUMP_INSN:
7875
      return base_cost - 10;
7876
 
7877
    default:
7878
      return base_cost + 10;
7879
    }
7880
}
7881
 
7882
/* Find the best place in the insn stream in the range
7883
   (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7884
   Create the barrier by inserting a jump and add a new fix entry for
7885
   it.  */
7886
static Mfix *
7887
create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7888
{
7889
  HOST_WIDE_INT count = 0;
7890
  rtx barrier;
7891
  rtx from = fix->insn;
7892
  /* The instruction after which we will insert the jump.  */
7893
  rtx selected = NULL;
7894
  int selected_cost;
7895
  /* The address at which the jump instruction will be placed.  */
7896
  HOST_WIDE_INT selected_address;
7897
  Mfix * new_fix;
7898
  HOST_WIDE_INT max_count = max_address - fix->address;
7899
  rtx label = gen_label_rtx ();
7900
 
7901
  selected_cost = arm_barrier_cost (from);
7902
  selected_address = fix->address;
7903
 
7904
  while (from && count < max_count)
7905
    {
7906
      rtx tmp;
7907
      int new_cost;
7908
 
7909
      /* This code shouldn't have been called if there was a natural barrier
7910
         within range.  */
7911
      gcc_assert (GET_CODE (from) != BARRIER);
7912
 
7913
      /* Count the length of this insn.  */
7914
      count += get_attr_length (from);
7915
 
7916
      /* If there is a jump table, add its length.  */
7917
      tmp = is_jump_table (from);
7918
      if (tmp != NULL)
7919
        {
7920
          count += get_jump_table_size (tmp);
7921
 
7922
          /* Jump tables aren't in a basic block, so base the cost on
7923
             the dispatch insn.  If we select this location, we will
7924
             still put the pool after the table.  */
7925
          new_cost = arm_barrier_cost (from);
7926
 
7927
          if (count < max_count
7928
              && (!selected || new_cost <= selected_cost))
7929
            {
7930
              selected = tmp;
7931
              selected_cost = new_cost;
7932
              selected_address = fix->address + count;
7933
            }
7934
 
7935
          /* Continue after the dispatch table.  */
7936
          from = NEXT_INSN (tmp);
7937
          continue;
7938
        }
7939
 
7940
      new_cost = arm_barrier_cost (from);
7941
 
7942
      if (count < max_count
7943
          && (!selected || new_cost <= selected_cost))
7944
        {
7945
          selected = from;
7946
          selected_cost = new_cost;
7947
          selected_address = fix->address + count;
7948
        }
7949
 
7950
      from = NEXT_INSN (from);
7951
    }
7952
 
7953
  /* Make sure that we found a place to insert the jump.  */
7954
  gcc_assert (selected);
7955
 
7956
  /* Create a new JUMP_INSN that branches around a barrier.  */
7957
  from = emit_jump_insn_after (gen_jump (label), selected);
7958
  JUMP_LABEL (from) = label;
7959
  barrier = emit_barrier_after (from);
7960
  emit_label_after (label, barrier);
7961
 
7962
  /* Create a minipool barrier entry for the new barrier.  */
7963
  new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7964
  new_fix->insn = barrier;
7965
  new_fix->address = selected_address;
7966
  new_fix->next = fix->next;
7967
  fix->next = new_fix;
7968
 
7969
  return new_fix;
7970
}
7971
 
7972
/* Record that there is a natural barrier in the insn stream at
7973
   ADDRESS.  */
7974
static void
7975
push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7976
{
7977
  Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7978
 
7979
  fix->insn = insn;
7980
  fix->address = address;
7981
 
7982
  fix->next = NULL;
7983
  if (minipool_fix_head != NULL)
7984
    minipool_fix_tail->next = fix;
7985
  else
7986
    minipool_fix_head = fix;
7987
 
7988
  minipool_fix_tail = fix;
7989
}
7990
 
7991
/* Record INSN, which will need fixing up to load a value from the
7992
   minipool.  ADDRESS is the offset of the insn since the start of the
7993
   function; LOC is a pointer to the part of the insn which requires
7994
   fixing; VALUE is the constant that must be loaded, which is of type
7995
   MODE.  */
7996
static void
7997
push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7998
                   enum machine_mode mode, rtx value)
7999
{
8000
  Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
8001
 
8002
#ifdef AOF_ASSEMBLER
8003
  /* PIC symbol references need to be converted into offsets into the
8004
     based area.  */
8005
  /* XXX This shouldn't be done here.  */
8006
  if (flag_pic && GET_CODE (value) == SYMBOL_REF)
8007
    value = aof_pic_entry (value);
8008
#endif /* AOF_ASSEMBLER */
8009
 
8010
  fix->insn = insn;
8011
  fix->address = address;
8012
  fix->loc = loc;
8013
  fix->mode = mode;
8014
  fix->fix_size = MINIPOOL_FIX_SIZE (mode);
8015
  fix->value = value;
8016
  fix->forwards = get_attr_pool_range (insn);
8017
  fix->backwards = get_attr_neg_pool_range (insn);
8018
  fix->minipool = NULL;
8019
 
8020
  /* If an insn doesn't have a range defined for it, then it isn't
8021
     expecting to be reworked by this code.  Better to stop now than
8022
     to generate duff assembly code.  */
8023
  gcc_assert (fix->forwards || fix->backwards);
8024
 
8025
  /* If an entry requires 8-byte alignment then assume all constant pools
8026
     require 4 bytes of padding.  Trying to do this later on a per-pool
8027
     basis is awkward because existing pool entries have to be modified.  */
8028
  if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
8029
    minipool_pad = 4;
8030
 
8031
  if (dump_file)
8032
    {
8033
      fprintf (dump_file,
8034
               ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
8035
               GET_MODE_NAME (mode),
8036
               INSN_UID (insn), (unsigned long) address,
8037
               -1 * (long)fix->backwards, (long)fix->forwards);
8038
      arm_print_value (dump_file, fix->value);
8039
      fprintf (dump_file, "\n");
8040
    }
8041
 
8042
  /* Add it to the chain of fixes.  */
8043
  fix->next = NULL;
8044
 
8045
  if (minipool_fix_head != NULL)
8046
    minipool_fix_tail->next = fix;
8047
  else
8048
    minipool_fix_head = fix;
8049
 
8050
  minipool_fix_tail = fix;
8051
}
8052
 
8053
/* Return the cost of synthesizing a 64-bit constant VAL inline.
8054
   Returns the number of insns needed, or 99 if we don't know how to
8055
   do it.  */
8056
int
8057
arm_const_double_inline_cost (rtx val)
8058
{
8059
  rtx lowpart, highpart;
8060
  enum machine_mode mode;
8061
 
8062
  mode = GET_MODE (val);
8063
 
8064
  if (mode == VOIDmode)
8065
    mode = DImode;
8066
 
8067
  gcc_assert (GET_MODE_SIZE (mode) == 8);
8068
 
8069
  lowpart = gen_lowpart (SImode, val);
8070
  highpart = gen_highpart_mode (SImode, mode, val);
8071
 
8072
  gcc_assert (GET_CODE (lowpart) == CONST_INT);
8073
  gcc_assert (GET_CODE (highpart) == CONST_INT);
8074
 
8075
  return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8076
                            NULL_RTX, NULL_RTX, 0, 0)
8077
          + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8078
                              NULL_RTX, NULL_RTX, 0, 0));
8079
}
8080
 
8081
/* Return true if it is worthwhile to split a 64-bit constant into two
8082
   32-bit operations.  This is the case if optimizing for size, or
8083
   if we have load delay slots, or if one 32-bit part can be done with
8084
   a single data operation.  */
8085
bool
8086
arm_const_double_by_parts (rtx val)
8087
{
8088
  enum machine_mode mode = GET_MODE (val);
8089
  rtx part;
8090
 
8091
  if (optimize_size || arm_ld_sched)
8092
    return true;
8093
 
8094
  if (mode == VOIDmode)
8095
    mode = DImode;
8096
 
8097
  part = gen_highpart_mode (SImode, mode, val);
8098
 
8099
  gcc_assert (GET_CODE (part) == CONST_INT);
8100
 
8101
  if (const_ok_for_arm (INTVAL (part))
8102
      || const_ok_for_arm (~INTVAL (part)))
8103
    return true;
8104
 
8105
  part = gen_lowpart (SImode, val);
8106
 
8107
  gcc_assert (GET_CODE (part) == CONST_INT);
8108
 
8109
  if (const_ok_for_arm (INTVAL (part))
8110
      || const_ok_for_arm (~INTVAL (part)))
8111
    return true;
8112
 
8113
  return false;
8114
}
8115
 
8116
/* Scan INSN and note any of its operands that need fixing.
8117
   If DO_PUSHES is false we do not actually push any of the fixups
8118
   needed.  The function returns TRUE if any fixups were needed/pushed.
8119
   This is used by arm_memory_load_p() which needs to know about loads
8120
   of constants that will be converted into minipool loads.  */
8121
static bool
8122
note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8123
{
8124
  bool result = false;
8125
  int opno;
8126
 
8127
  extract_insn (insn);
8128
 
8129
  if (!constrain_operands (1))
8130
    fatal_insn_not_found (insn);
8131
 
8132
  if (recog_data.n_alternatives == 0)
8133
    return false;
8134
 
8135
  /* Fill in recog_op_alt with information about the constraints of
8136
     this insn.  */
8137
  preprocess_constraints ();
8138
 
8139
  for (opno = 0; opno < recog_data.n_operands; opno++)
8140
    {
8141
      /* Things we need to fix can only occur in inputs.  */
8142
      if (recog_data.operand_type[opno] != OP_IN)
8143
        continue;
8144
 
8145
      /* If this alternative is a memory reference, then any mention
8146
         of constants in this alternative is really to fool reload
8147
         into allowing us to accept one there.  We need to fix them up
8148
         now so that we output the right code.  */
8149
      if (recog_op_alt[opno][which_alternative].memory_ok)
8150
        {
8151
          rtx op = recog_data.operand[opno];
8152
 
8153
          if (CONSTANT_P (op))
8154
            {
8155
              if (do_pushes)
8156
                push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8157
                                   recog_data.operand_mode[opno], op);
8158
              result = true;
8159
            }
8160
          else if (GET_CODE (op) == MEM
8161
                   && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8162
                   && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8163
            {
8164
              if (do_pushes)
8165
                {
8166
                  rtx cop = avoid_constant_pool_reference (op);
8167
 
8168
                  /* Casting the address of something to a mode narrower
8169
                     than a word can cause avoid_constant_pool_reference()
8170
                     to return the pool reference itself.  That's no good to
8171
                     us here.  Lets just hope that we can use the
8172
                     constant pool value directly.  */
8173
                  if (op == cop)
8174
                    cop = get_pool_constant (XEXP (op, 0));
8175
 
8176
                  push_minipool_fix (insn, address,
8177
                                     recog_data.operand_loc[opno],
8178
                                     recog_data.operand_mode[opno], cop);
8179
                }
8180
 
8181
              result = true;
8182
            }
8183
        }
8184
    }
8185
 
8186
  return result;
8187
}
8188
 
8189
/* Gcc puts the pool in the wrong place for ARM, since we can only
8190
   load addresses a limited distance around the pc.  We do some
8191
   special munging to move the constant pool values to the correct
8192
   point in the code.  */
8193
static void
8194
arm_reorg (void)
8195
{
8196
  rtx insn;
8197
  HOST_WIDE_INT address = 0;
8198
  Mfix * fix;
8199
 
8200
  minipool_fix_head = minipool_fix_tail = NULL;
8201
 
8202
  /* The first insn must always be a note, or the code below won't
8203
     scan it properly.  */
8204
  insn = get_insns ();
8205
  gcc_assert (GET_CODE (insn) == NOTE);
8206
  minipool_pad = 0;
8207
 
8208
  /* Scan all the insns and record the operands that will need fixing.  */
8209
  for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8210
    {
8211
      if (TARGET_CIRRUS_FIX_INVALID_INSNS
8212
          && (arm_cirrus_insn_p (insn)
8213
              || GET_CODE (insn) == JUMP_INSN
8214
              || arm_memory_load_p (insn)))
8215
        cirrus_reorg (insn);
8216
 
8217
      if (GET_CODE (insn) == BARRIER)
8218
        push_minipool_barrier (insn, address);
8219
      else if (INSN_P (insn))
8220
        {
8221
          rtx table;
8222
 
8223
          note_invalid_constants (insn, address, true);
8224
          address += get_attr_length (insn);
8225
 
8226
          /* If the insn is a vector jump, add the size of the table
8227
             and skip the table.  */
8228
          if ((table = is_jump_table (insn)) != NULL)
8229
            {
8230
              address += get_jump_table_size (table);
8231
              insn = table;
8232
            }
8233
        }
8234
    }
8235
 
8236
  fix = minipool_fix_head;
8237
 
8238
  /* Now scan the fixups and perform the required changes.  */
8239
  while (fix)
8240
    {
8241
      Mfix * ftmp;
8242
      Mfix * fdel;
8243
      Mfix *  last_added_fix;
8244
      Mfix * last_barrier = NULL;
8245
      Mfix * this_fix;
8246
 
8247
      /* Skip any further barriers before the next fix.  */
8248
      while (fix && GET_CODE (fix->insn) == BARRIER)
8249
        fix = fix->next;
8250
 
8251
      /* No more fixes.  */
8252
      if (fix == NULL)
8253
        break;
8254
 
8255
      last_added_fix = NULL;
8256
 
8257
      for (ftmp = fix; ftmp; ftmp = ftmp->next)
8258
        {
8259
          if (GET_CODE (ftmp->insn) == BARRIER)
8260
            {
8261
              if (ftmp->address >= minipool_vector_head->max_address)
8262
                break;
8263
 
8264
              last_barrier = ftmp;
8265
            }
8266
          else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8267
            break;
8268
 
8269
          last_added_fix = ftmp;  /* Keep track of the last fix added.  */
8270
        }
8271
 
8272
      /* If we found a barrier, drop back to that; any fixes that we
8273
         could have reached but come after the barrier will now go in
8274
         the next mini-pool.  */
8275
      if (last_barrier != NULL)
8276
        {
8277
          /* Reduce the refcount for those fixes that won't go into this
8278
             pool after all.  */
8279
          for (fdel = last_barrier->next;
8280
               fdel && fdel != ftmp;
8281
               fdel = fdel->next)
8282
            {
8283
              fdel->minipool->refcount--;
8284
              fdel->minipool = NULL;
8285
            }
8286
 
8287
          ftmp = last_barrier;
8288
        }
8289
      else
8290
        {
8291
          /* ftmp is first fix that we can't fit into this pool and
8292
             there no natural barriers that we could use.  Insert a
8293
             new barrier in the code somewhere between the previous
8294
             fix and this one, and arrange to jump around it.  */
8295
          HOST_WIDE_INT max_address;
8296
 
8297
          /* The last item on the list of fixes must be a barrier, so
8298
             we can never run off the end of the list of fixes without
8299
             last_barrier being set.  */
8300
          gcc_assert (ftmp);
8301
 
8302
          max_address = minipool_vector_head->max_address;
8303
          /* Check that there isn't another fix that is in range that
8304
             we couldn't fit into this pool because the pool was
8305
             already too large: we need to put the pool before such an
8306
             instruction.  The pool itself may come just after the
8307
             fix because create_fix_barrier also allows space for a
8308
             jump instruction.  */
8309
          if (ftmp->address < max_address)
8310
            max_address = ftmp->address + 1;
8311
 
8312
          last_barrier = create_fix_barrier (last_added_fix, max_address);
8313
        }
8314
 
8315
      assign_minipool_offsets (last_barrier);
8316
 
8317
      while (ftmp)
8318
        {
8319
          if (GET_CODE (ftmp->insn) != BARRIER
8320
              && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8321
                  == NULL))
8322
            break;
8323
 
8324
          ftmp = ftmp->next;
8325
        }
8326
 
8327
      /* Scan over the fixes we have identified for this pool, fixing them
8328
         up and adding the constants to the pool itself.  */
8329
      for (this_fix = fix; this_fix && ftmp != this_fix;
8330
           this_fix = this_fix->next)
8331
        if (GET_CODE (this_fix->insn) != BARRIER)
8332
          {
8333
            rtx addr
8334
              = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8335
                                                  minipool_vector_label),
8336
                               this_fix->minipool->offset);
8337
            *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8338
          }
8339
 
8340
      dump_minipool (last_barrier->insn);
8341
      fix = ftmp;
8342
    }
8343
 
8344
  /* From now on we must synthesize any constants that we can't handle
8345
     directly.  This can happen if the RTL gets split during final
8346
     instruction generation.  */
8347
  after_arm_reorg = 1;
8348
 
8349
  /* Free the minipool memory.  */
8350
  obstack_free (&minipool_obstack, minipool_startobj);
8351
}
8352
 
8353
/* Routines to output assembly language.  */
8354
 
8355
/* If the rtx is the correct value then return the string of the number.
8356
   In this way we can ensure that valid double constants are generated even
8357
   when cross compiling.  */
8358
const char *
8359
fp_immediate_constant (rtx x)
8360
{
8361
  REAL_VALUE_TYPE r;
8362
  int i;
8363
 
8364
  if (!fp_consts_inited)
8365
    init_fp_table ();
8366
 
8367
  REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8368
  for (i = 0; i < 8; i++)
8369
    if (REAL_VALUES_EQUAL (r, values_fp[i]))
8370
      return strings_fp[i];
8371
 
8372
  gcc_unreachable ();
8373
}
8374
 
8375
/* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
8376
static const char *
8377
fp_const_from_val (REAL_VALUE_TYPE *r)
8378
{
8379
  int i;
8380
 
8381
  if (!fp_consts_inited)
8382
    init_fp_table ();
8383
 
8384
  for (i = 0; i < 8; i++)
8385
    if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8386
      return strings_fp[i];
8387
 
8388
  gcc_unreachable ();
8389
}
8390
 
8391
/* Output the operands of a LDM/STM instruction to STREAM.
8392
   MASK is the ARM register set mask of which only bits 0-15 are important.
8393
   REG is the base register, either the frame pointer or the stack pointer,
8394
   INSTR is the possibly suffixed load or store instruction.  */
8395
 
8396
static void
8397
print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8398
                 unsigned long mask)
8399
{
8400
  unsigned i;
8401
  bool not_first = FALSE;
8402
 
8403
  fputc ('\t', stream);
8404
  asm_fprintf (stream, instr, reg);
8405
  fputs (", {", stream);
8406
 
8407
  for (i = 0; i <= LAST_ARM_REGNUM; i++)
8408
    if (mask & (1 << i))
8409
      {
8410
        if (not_first)
8411
          fprintf (stream, ", ");
8412
 
8413
        asm_fprintf (stream, "%r", i);
8414
        not_first = TRUE;
8415
      }
8416
 
8417
  fprintf (stream, "}\n");
8418
}
8419
 
8420
 
8421
/* Output a FLDMX instruction to STREAM.
8422
   BASE if the register containing the address.
8423
   REG and COUNT specify the register range.
8424
   Extra registers may be added to avoid hardware bugs.  */
8425
 
8426
static void
8427
arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
8428
{
8429
  int i;
8430
 
8431
  /* Workaround ARM10 VFPr1 bug.  */
8432
  if (count == 2 && !arm_arch6)
8433
    {
8434
      if (reg == 15)
8435
        reg--;
8436
      count++;
8437
    }
8438
 
8439
  fputc ('\t', stream);
8440
  asm_fprintf (stream, "fldmfdx\t%r!, {", base);
8441
 
8442
  for (i = reg; i < reg + count; i++)
8443
    {
8444
      if (i > reg)
8445
        fputs (", ", stream);
8446
      asm_fprintf (stream, "d%d", i);
8447
    }
8448
  fputs ("}\n", stream);
8449
 
8450
}
8451
 
8452
 
8453
/* Output the assembly for a store multiple.  */
8454
 
8455
const char *
8456
vfp_output_fstmx (rtx * operands)
8457
{
8458
  char pattern[100];
8459
  int p;
8460
  int base;
8461
  int i;
8462
 
8463
  strcpy (pattern, "fstmfdx\t%m0!, {%P1");
8464
  p = strlen (pattern);
8465
 
8466
  gcc_assert (GET_CODE (operands[1]) == REG);
8467
 
8468
  base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8469
  for (i = 1; i < XVECLEN (operands[2], 0); i++)
8470
    {
8471
      p += sprintf (&pattern[p], ", d%d", base + i);
8472
    }
8473
  strcpy (&pattern[p], "}");
8474
 
8475
  output_asm_insn (pattern, operands);
8476
  return "";
8477
}
8478
 
8479
 
8480
/* Emit RTL to save block of VFP register pairs to the stack.  Returns the
8481
   number of bytes pushed.  */
8482
 
8483
static int
8484
vfp_emit_fstmx (int base_reg, int count)
8485
{
8486
  rtx par;
8487
  rtx dwarf;
8488
  rtx tmp, reg;
8489
  int i;
8490
 
8491
  /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
8492
     register pairs are stored by a store multiple insn.  We avoid this
8493
     by pushing an extra pair.  */
8494
  if (count == 2 && !arm_arch6)
8495
    {
8496
      if (base_reg == LAST_VFP_REGNUM - 3)
8497
        base_reg -= 2;
8498
      count++;
8499
    }
8500
 
8501
  /* ??? The frame layout is implementation defined.  We describe
8502
     standard format 1 (equivalent to a FSTMD insn and unused pad word).
8503
     We really need some way of representing the whole block so that the
8504
     unwinder can figure it out at runtime.  */
8505
  par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8506
  dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8507
 
8508
  reg = gen_rtx_REG (DFmode, base_reg);
8509
  base_reg += 2;
8510
 
8511
  XVECEXP (par, 0, 0)
8512
    = gen_rtx_SET (VOIDmode,
8513
                   gen_frame_mem (BLKmode,
8514
                                  gen_rtx_PRE_DEC (BLKmode,
8515
                                                   stack_pointer_rtx)),
8516
                   gen_rtx_UNSPEC (BLKmode,
8517
                                   gen_rtvec (1, reg),
8518
                                   UNSPEC_PUSH_MULT));
8519
 
8520
  tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8521
                     plus_constant (stack_pointer_rtx, -(count * 8 + 4)));
8522
  RTX_FRAME_RELATED_P (tmp) = 1;
8523
  XVECEXP (dwarf, 0, 0) = tmp;
8524
 
8525
  tmp = gen_rtx_SET (VOIDmode,
8526
                     gen_frame_mem (DFmode, stack_pointer_rtx),
8527
                     reg);
8528
  RTX_FRAME_RELATED_P (tmp) = 1;
8529
  XVECEXP (dwarf, 0, 1) = tmp;
8530
 
8531
  for (i = 1; i < count; i++)
8532
    {
8533
      reg = gen_rtx_REG (DFmode, base_reg);
8534
      base_reg += 2;
8535
      XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8536
 
8537
      tmp = gen_rtx_SET (VOIDmode,
8538
                         gen_frame_mem (DFmode,
8539
                                        plus_constant (stack_pointer_rtx,
8540
                                                       i * 8)),
8541
                         reg);
8542
      RTX_FRAME_RELATED_P (tmp) = 1;
8543
      XVECEXP (dwarf, 0, i + 1) = tmp;
8544
    }
8545
 
8546
  par = emit_insn (par);
8547
  REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8548
                                       REG_NOTES (par));
8549
  RTX_FRAME_RELATED_P (par) = 1;
8550
 
8551
  return count * 8 + 4;
8552
}
8553
 
8554
 
8555
/* Output a 'call' insn.  */
8556
const char *
8557
output_call (rtx *operands)
8558
{
8559
  gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
8560
 
8561
  /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8562
  if (REGNO (operands[0]) == LR_REGNUM)
8563
    {
8564
      operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8565
      output_asm_insn ("mov%?\t%0, %|lr", operands);
8566
    }
8567
 
8568
  output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8569
 
8570
  if (TARGET_INTERWORK || arm_arch4t)
8571
    output_asm_insn ("bx%?\t%0", operands);
8572
  else
8573
    output_asm_insn ("mov%?\t%|pc, %0", operands);
8574
 
8575
  return "";
8576
}
8577
 
8578
/* Output a 'call' insn that is a reference in memory.  */
8579
const char *
8580
output_call_mem (rtx *operands)
8581
{
8582
  if (TARGET_INTERWORK && !arm_arch5)
8583
    {
8584
      output_asm_insn ("ldr%?\t%|ip, %0", operands);
8585
      output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8586
      output_asm_insn ("bx%?\t%|ip", operands);
8587
    }
8588
  else if (regno_use_in (LR_REGNUM, operands[0]))
8589
    {
8590
      /* LR is used in the memory address.  We load the address in the
8591
         first instruction.  It's safe to use IP as the target of the
8592
         load since the call will kill it anyway.  */
8593
      output_asm_insn ("ldr%?\t%|ip, %0", operands);
8594
      if (arm_arch5)
8595
        output_asm_insn ("blx%?\t%|ip", operands);
8596
      else
8597
        {
8598
          output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8599
          if (arm_arch4t)
8600
            output_asm_insn ("bx%?\t%|ip", operands);
8601
          else
8602
            output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8603
        }
8604
    }
8605
  else
8606
    {
8607
      output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8608
      output_asm_insn ("ldr%?\t%|pc, %0", operands);
8609
    }
8610
 
8611
  return "";
8612
}
8613
 
8614
 
8615
/* Output a move from arm registers to an fpa registers.
8616
   OPERANDS[0] is an fpa register.
8617
   OPERANDS[1] is the first registers of an arm register pair.  */
8618
const char *
8619
output_mov_long_double_fpa_from_arm (rtx *operands)
8620
{
8621
  int arm_reg0 = REGNO (operands[1]);
8622
  rtx ops[3];
8623
 
8624
  gcc_assert (arm_reg0 != IP_REGNUM);
8625
 
8626
  ops[0] = gen_rtx_REG (SImode, arm_reg0);
8627
  ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8628
  ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8629
 
8630
  output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8631
  output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8632
 
8633
  return "";
8634
}
8635
 
8636
/* Output a move from an fpa register to arm registers.
8637
   OPERANDS[0] is the first registers of an arm register pair.
8638
   OPERANDS[1] is an fpa register.  */
8639
const char *
8640
output_mov_long_double_arm_from_fpa (rtx *operands)
8641
{
8642
  int arm_reg0 = REGNO (operands[0]);
8643
  rtx ops[3];
8644
 
8645
  gcc_assert (arm_reg0 != IP_REGNUM);
8646
 
8647
  ops[0] = gen_rtx_REG (SImode, arm_reg0);
8648
  ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8649
  ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8650
 
8651
  output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8652
  output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8653
  return "";
8654
}
8655
 
8656
/* Output a move from arm registers to arm registers of a long double
8657
   OPERANDS[0] is the destination.
8658
   OPERANDS[1] is the source.  */
8659
const char *
8660
output_mov_long_double_arm_from_arm (rtx *operands)
8661
{
8662
  /* We have to be careful here because the two might overlap.  */
8663
  int dest_start = REGNO (operands[0]);
8664
  int src_start = REGNO (operands[1]);
8665
  rtx ops[2];
8666
  int i;
8667
 
8668
  if (dest_start < src_start)
8669
    {
8670
      for (i = 0; i < 3; i++)
8671
        {
8672
          ops[0] = gen_rtx_REG (SImode, dest_start + i);
8673
          ops[1] = gen_rtx_REG (SImode, src_start + i);
8674
          output_asm_insn ("mov%?\t%0, %1", ops);
8675
        }
8676
    }
8677
  else
8678
    {
8679
      for (i = 2; i >= 0; i--)
8680
        {
8681
          ops[0] = gen_rtx_REG (SImode, dest_start + i);
8682
          ops[1] = gen_rtx_REG (SImode, src_start + i);
8683
          output_asm_insn ("mov%?\t%0, %1", ops);
8684
        }
8685
    }
8686
 
8687
  return "";
8688
}
8689
 
8690
 
8691
/* Output a move from arm registers to an fpa registers.
8692
   OPERANDS[0] is an fpa register.
8693
   OPERANDS[1] is the first registers of an arm register pair.  */
8694
const char *
8695
output_mov_double_fpa_from_arm (rtx *operands)
8696
{
8697
  int arm_reg0 = REGNO (operands[1]);
8698
  rtx ops[2];
8699
 
8700
  gcc_assert (arm_reg0 != IP_REGNUM);
8701
 
8702
  ops[0] = gen_rtx_REG (SImode, arm_reg0);
8703
  ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8704
  output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8705
  output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8706
  return "";
8707
}
8708
 
8709
/* Output a move from an fpa register to arm registers.
8710
   OPERANDS[0] is the first registers of an arm register pair.
8711
   OPERANDS[1] is an fpa register.  */
8712
const char *
8713
output_mov_double_arm_from_fpa (rtx *operands)
8714
{
8715
  int arm_reg0 = REGNO (operands[0]);
8716
  rtx ops[2];
8717
 
8718
  gcc_assert (arm_reg0 != IP_REGNUM);
8719
 
8720
  ops[0] = gen_rtx_REG (SImode, arm_reg0);
8721
  ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8722
  output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8723
  output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8724
  return "";
8725
}
8726
 
8727
/* Output a move between double words.
8728
   It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8729
   or MEM<-REG and all MEMs must be offsettable addresses.  */
8730
const char *
8731
output_move_double (rtx *operands)
8732
{
8733
  enum rtx_code code0 = GET_CODE (operands[0]);
8734
  enum rtx_code code1 = GET_CODE (operands[1]);
8735
  rtx otherops[3];
8736
 
8737
  if (code0 == REG)
8738
    {
8739
      int reg0 = REGNO (operands[0]);
8740
 
8741
      otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8742
 
8743
      gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
8744
 
8745
      switch (GET_CODE (XEXP (operands[1], 0)))
8746
        {
8747
        case REG:
8748
          output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8749
          break;
8750
 
8751
        case PRE_INC:
8752
          gcc_assert (TARGET_LDRD);
8753
          output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8754
          break;
8755
 
8756
        case PRE_DEC:
8757
          output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8758
          break;
8759
 
8760
        case POST_INC:
8761
          output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8762
          break;
8763
 
8764
        case POST_DEC:
8765
          gcc_assert (TARGET_LDRD);
8766
          output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8767
          break;
8768
 
8769
        case PRE_MODIFY:
8770
        case POST_MODIFY:
8771
          otherops[0] = operands[0];
8772
          otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8773
          otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8774
 
8775
          if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8776
            {
8777
              if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8778
                {
8779
                  /* Registers overlap so split out the increment.  */
8780
                  output_asm_insn ("add%?\t%1, %1, %2", otherops);
8781
                  output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8782
                }
8783
              else
8784
                {
8785
                  /* IWMMXT allows offsets larger than ldrd can handle,
8786
                     fix these up with a pair of ldr.  */
8787
                  if (GET_CODE (otherops[2]) == CONST_INT
8788
                      && (INTVAL(otherops[2]) <= -256
8789
                          || INTVAL(otherops[2]) >= 256))
8790
                    {
8791
                      output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
8792
                      otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8793
                      output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
8794
                    }
8795
                  else
8796
                    output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8797
                }
8798
            }
8799
          else
8800
            {
8801
              /* IWMMXT allows offsets larger than ldrd can handle,
8802
                 fix these up with a pair of ldr.  */
8803
              if (GET_CODE (otherops[2]) == CONST_INT
8804
                  && (INTVAL(otherops[2]) <= -256
8805
                      || INTVAL(otherops[2]) >= 256))
8806
                {
8807
                  otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8808
                  output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
8809
                  otherops[0] = operands[0];
8810
                  output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
8811
                }
8812
              else
8813
                /* We only allow constant increments, so this is safe.  */
8814
                output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8815
            }
8816
          break;
8817
 
8818
        case LABEL_REF:
8819
        case CONST:
8820
          output_asm_insn ("adr%?\t%0, %1", operands);
8821
          output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8822
          break;
8823
 
8824
        default:
8825
          if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8826
                               GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8827
            {
8828
              otherops[0] = operands[0];
8829
              otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8830
              otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8831
 
8832
              if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8833
                {
8834
                  if (GET_CODE (otherops[2]) == CONST_INT)
8835
                    {
8836
                      switch ((int) INTVAL (otherops[2]))
8837
                        {
8838
                        case -8:
8839
                          output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8840
                          return "";
8841
                        case -4:
8842
                          output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8843
                          return "";
8844
                        case 4:
8845
                          output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8846
                          return "";
8847
                        }
8848
                    }
8849
                  if (TARGET_LDRD
8850
                      && (GET_CODE (otherops[2]) == REG
8851
                          || (GET_CODE (otherops[2]) == CONST_INT
8852
                              && INTVAL (otherops[2]) > -256
8853
                              && INTVAL (otherops[2]) < 256)))
8854
                    {
8855
                      if (reg_overlap_mentioned_p (otherops[0],
8856
                                                   otherops[2]))
8857
                        {
8858
                          /* Swap base and index registers over to
8859
                             avoid a conflict.  */
8860
                          otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8861
                          otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8862
                        }
8863
                      /* If both registers conflict, it will usually
8864
                         have been fixed by a splitter.  */
8865
                      if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8866
                        {
8867
                          output_asm_insn ("add%?\t%1, %1, %2", otherops);
8868
                          output_asm_insn ("ldr%?d\t%0, [%1]",
8869
                                           otherops);
8870
                        }
8871
                      else
8872
                        output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8873
                      return "";
8874
                    }
8875
 
8876
                  if (GET_CODE (otherops[2]) == CONST_INT)
8877
                    {
8878
                      if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8879
                        output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8880
                      else
8881
                        output_asm_insn ("add%?\t%0, %1, %2", otherops);
8882
                    }
8883
                  else
8884
                    output_asm_insn ("add%?\t%0, %1, %2", otherops);
8885
                }
8886
              else
8887
                output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8888
 
8889
              return "ldm%?ia\t%0, %M0";
8890
            }
8891
          else
8892
            {
8893
              otherops[1] = adjust_address (operands[1], SImode, 4);
8894
              /* Take care of overlapping base/data reg.  */
8895
              if (reg_mentioned_p (operands[0], operands[1]))
8896
                {
8897
                  output_asm_insn ("ldr%?\t%0, %1", otherops);
8898
                  output_asm_insn ("ldr%?\t%0, %1", operands);
8899
                }
8900
              else
8901
                {
8902
                  output_asm_insn ("ldr%?\t%0, %1", operands);
8903
                  output_asm_insn ("ldr%?\t%0, %1", otherops);
8904
                }
8905
            }
8906
        }
8907
    }
8908
  else
8909
    {
8910
      /* Constraints should ensure this.  */
8911
      gcc_assert (code0 == MEM && code1 == REG);
8912
      gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8913
 
8914
      switch (GET_CODE (XEXP (operands[0], 0)))
8915
        {
8916
        case REG:
8917
          output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8918
          break;
8919
 
8920
        case PRE_INC:
8921
          gcc_assert (TARGET_LDRD);
8922
          output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8923
          break;
8924
 
8925
        case PRE_DEC:
8926
          output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8927
          break;
8928
 
8929
        case POST_INC:
8930
          output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8931
          break;
8932
 
8933
        case POST_DEC:
8934
          gcc_assert (TARGET_LDRD);
8935
          output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8936
          break;
8937
 
8938
        case PRE_MODIFY:
8939
        case POST_MODIFY:
8940
          otherops[0] = operands[1];
8941
          otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8942
          otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8943
 
8944
          /* IWMMXT allows offsets larger than ldrd can handle,
8945
             fix these up with a pair of ldr.  */
8946
          if (GET_CODE (otherops[2]) == CONST_INT
8947
              && (INTVAL(otherops[2]) <= -256
8948
                  || INTVAL(otherops[2]) >= 256))
8949
            {
8950
              rtx reg1;
8951
              reg1 = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8952
              if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8953
                {
8954
                  output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
8955
                  otherops[0] = reg1;
8956
                  output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
8957
                }
8958
              else
8959
                {
8960
                  otherops[0] = reg1;
8961
                  output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
8962
                  otherops[0] = operands[1];
8963
                  output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
8964
                }
8965
            }
8966
          else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8967
            output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8968
          else
8969
            output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8970
          break;
8971
 
8972
        case PLUS:
8973
          otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8974
          if (GET_CODE (otherops[2]) == CONST_INT)
8975
            {
8976
              switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8977
                {
8978
                case -8:
8979
                  output_asm_insn ("stm%?db\t%m0, %M1", operands);
8980
                  return "";
8981
 
8982
                case -4:
8983
                  output_asm_insn ("stm%?da\t%m0, %M1", operands);
8984
                  return "";
8985
 
8986
                case 4:
8987
                  output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8988
                  return "";
8989
                }
8990
            }
8991
          if (TARGET_LDRD
8992
              && (GET_CODE (otherops[2]) == REG
8993
                  || (GET_CODE (otherops[2]) == CONST_INT
8994
                      && INTVAL (otherops[2]) > -256
8995
                      && INTVAL (otherops[2]) < 256)))
8996
            {
8997
              otherops[0] = operands[1];
8998
              otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8999
              output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
9000
              return "";
9001
            }
9002
          /* Fall through */
9003
 
9004
        default:
9005
          otherops[0] = adjust_address (operands[0], SImode, 4);
9006
          otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9007
          output_asm_insn ("str%?\t%1, %0", operands);
9008
          output_asm_insn ("str%?\t%1, %0", otherops);
9009
        }
9010
    }
9011
 
9012
  return "";
9013
}
9014
 
9015
/* Output an ADD r, s, #n where n may be too big for one instruction.
9016
   If adding zero to one register, output nothing.  */
9017
const char *
9018
output_add_immediate (rtx *operands)
9019
{
9020
  HOST_WIDE_INT n = INTVAL (operands[2]);
9021
 
9022
  if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
9023
    {
9024
      if (n < 0)
9025
        output_multi_immediate (operands,
9026
                                "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
9027
                                -n);
9028
      else
9029
        output_multi_immediate (operands,
9030
                                "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
9031
                                n);
9032
    }
9033
 
9034
  return "";
9035
}
9036
 
9037
/* Output a multiple immediate operation.
9038
   OPERANDS is the vector of operands referred to in the output patterns.
9039
   INSTR1 is the output pattern to use for the first constant.
9040
   INSTR2 is the output pattern to use for subsequent constants.
9041
   IMMED_OP is the index of the constant slot in OPERANDS.
9042
   N is the constant value.  */
9043
static const char *
9044
output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
9045
                        int immed_op, HOST_WIDE_INT n)
9046
{
9047
#if HOST_BITS_PER_WIDE_INT > 32
9048
  n &= 0xffffffff;
9049
#endif
9050
 
9051
  if (n == 0)
9052
    {
9053
      /* Quick and easy output.  */
9054
      operands[immed_op] = const0_rtx;
9055
      output_asm_insn (instr1, operands);
9056
    }
9057
  else
9058
    {
9059
      int i;
9060
      const char * instr = instr1;
9061
 
9062
      /* Note that n is never zero here (which would give no output).  */
9063
      for (i = 0; i < 32; i += 2)
9064
        {
9065
          if (n & (3 << i))
9066
            {
9067
              operands[immed_op] = GEN_INT (n & (255 << i));
9068
              output_asm_insn (instr, operands);
9069
              instr = instr2;
9070
              i += 6;
9071
            }
9072
        }
9073
    }
9074
 
9075
  return "";
9076
}
9077
 
9078
/* Return the appropriate ARM instruction for the operation code.
9079
   The returned result should not be overwritten.  OP is the rtx of the
9080
   operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
9081
   was shifted.  */
9082
const char *
9083
arithmetic_instr (rtx op, int shift_first_arg)
9084
{
9085
  switch (GET_CODE (op))
9086
    {
9087
    case PLUS:
9088
      return "add";
9089
 
9090
    case MINUS:
9091
      return shift_first_arg ? "rsb" : "sub";
9092
 
9093
    case IOR:
9094
      return "orr";
9095
 
9096
    case XOR:
9097
      return "eor";
9098
 
9099
    case AND:
9100
      return "and";
9101
 
9102
    default:
9103
      gcc_unreachable ();
9104
    }
9105
}
9106
 
9107
/* Ensure valid constant shifts and return the appropriate shift mnemonic
9108
   for the operation code.  The returned result should not be overwritten.
9109
   OP is the rtx code of the shift.
9110
   On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
9111
   shift.  */
9112
static const char *
9113
shift_op (rtx op, HOST_WIDE_INT *amountp)
9114
{
9115
  const char * mnem;
9116
  enum rtx_code code = GET_CODE (op);
9117
 
9118
  switch (GET_CODE (XEXP (op, 1)))
9119
    {
9120
    case REG:
9121
    case SUBREG:
9122
      *amountp = -1;
9123
      break;
9124
 
9125
    case CONST_INT:
9126
      *amountp = INTVAL (XEXP (op, 1));
9127
      break;
9128
 
9129
    default:
9130
      gcc_unreachable ();
9131
    }
9132
 
9133
  switch (code)
9134
    {
9135
    case ASHIFT:
9136
      mnem = "asl";
9137
      break;
9138
 
9139
    case ASHIFTRT:
9140
      mnem = "asr";
9141
      break;
9142
 
9143
    case LSHIFTRT:
9144
      mnem = "lsr";
9145
      break;
9146
 
9147
    case ROTATE:
9148
      gcc_assert (*amountp != -1);
9149
      *amountp = 32 - *amountp;
9150
 
9151
      /* Fall through.  */
9152
 
9153
    case ROTATERT:
9154
      mnem = "ror";
9155
      break;
9156
 
9157
    case MULT:
9158
      /* We never have to worry about the amount being other than a
9159
         power of 2, since this case can never be reloaded from a reg.  */
9160
      gcc_assert (*amountp != -1);
9161
      *amountp = int_log2 (*amountp);
9162
      return "asl";
9163
 
9164
    default:
9165
      gcc_unreachable ();
9166
    }
9167
 
9168
  if (*amountp != -1)
9169
    {
9170
      /* This is not 100% correct, but follows from the desire to merge
9171
         multiplication by a power of 2 with the recognizer for a
9172
         shift.  >=32 is not a valid shift for "asl", so we must try and
9173
         output a shift that produces the correct arithmetical result.
9174
         Using lsr #32 is identical except for the fact that the carry bit
9175
         is not set correctly if we set the flags; but we never use the
9176
         carry bit from such an operation, so we can ignore that.  */
9177
      if (code == ROTATERT)
9178
        /* Rotate is just modulo 32.  */
9179
        *amountp &= 31;
9180
      else if (*amountp != (*amountp & 31))
9181
        {
9182
          if (code == ASHIFT)
9183
            mnem = "lsr";
9184
          *amountp = 32;
9185
        }
9186
 
9187
      /* Shifts of 0 are no-ops.  */
9188
      if (*amountp == 0)
9189
        return NULL;
9190
    }
9191
 
9192
  return mnem;
9193
}
9194
 
9195
/* Obtain the shift from the POWER of two.  */
9196
 
9197
static HOST_WIDE_INT
9198
int_log2 (HOST_WIDE_INT power)
9199
{
9200
  HOST_WIDE_INT shift = 0;
9201
 
9202
  while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9203
    {
9204
      gcc_assert (shift <= 31);
9205
      shift++;
9206
    }
9207
 
9208
  return shift;
9209
}
9210
 
9211
/* Output a .ascii pseudo-op, keeping track of lengths.  This is
9212
   because /bin/as is horribly restrictive.  The judgement about
9213
   whether or not each character is 'printable' (and can be output as
9214
   is) or not (and must be printed with an octal escape) must be made
9215
   with reference to the *host* character set -- the situation is
9216
   similar to that discussed in the comments above pp_c_char in
9217
   c-pretty-print.c.  */
9218
 
9219
#define MAX_ASCII_LEN 51
9220
 
9221
void
9222
output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9223
{
9224
  int i;
9225
  int len_so_far = 0;
9226
 
9227
  fputs ("\t.ascii\t\"", stream);
9228
 
9229
  for (i = 0; i < len; i++)
9230
    {
9231
      int c = p[i];
9232
 
9233
      if (len_so_far >= MAX_ASCII_LEN)
9234
        {
9235
          fputs ("\"\n\t.ascii\t\"", stream);
9236
          len_so_far = 0;
9237
        }
9238
 
9239
      if (ISPRINT (c))
9240
        {
9241
          if (c == '\\' || c == '\"')
9242
            {
9243
              putc ('\\', stream);
9244
              len_so_far++;
9245
            }
9246
          putc (c, stream);
9247
          len_so_far++;
9248
        }
9249
      else
9250
        {
9251
          fprintf (stream, "\\%03o", c);
9252
          len_so_far += 4;
9253
        }
9254
    }
9255
 
9256
  fputs ("\"\n", stream);
9257
}
9258
 
9259
/* Compute the register save mask for registers 0 through 12
9260
   inclusive.  This code is used by arm_compute_save_reg_mask.  */
9261
 
9262
static unsigned long
9263
arm_compute_save_reg0_reg12_mask (void)
9264
{
9265
  unsigned long func_type = arm_current_func_type ();
9266
  unsigned long save_reg_mask = 0;
9267
  unsigned int reg;
9268
 
9269
  if (IS_INTERRUPT (func_type))
9270
    {
9271
      unsigned int max_reg;
9272
      /* Interrupt functions must not corrupt any registers,
9273
         even call clobbered ones.  If this is a leaf function
9274
         we can just examine the registers used by the RTL, but
9275
         otherwise we have to assume that whatever function is
9276
         called might clobber anything, and so we have to save
9277
         all the call-clobbered registers as well.  */
9278
      if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9279
        /* FIQ handlers have registers r8 - r12 banked, so
9280
           we only need to check r0 - r7, Normal ISRs only
9281
           bank r14 and r15, so we must check up to r12.
9282
           r13 is the stack pointer which is always preserved,
9283
           so we do not need to consider it here.  */
9284
        max_reg = 7;
9285
      else
9286
        max_reg = 12;
9287
 
9288
      for (reg = 0; reg <= max_reg; reg++)
9289
        if (regs_ever_live[reg]
9290
            || (! current_function_is_leaf && call_used_regs [reg]))
9291
          save_reg_mask |= (1 << reg);
9292
 
9293
      /* Also save the pic base register if necessary.  */
9294
      if (flag_pic
9295
          && !TARGET_SINGLE_PIC_BASE
9296
          && arm_pic_register != INVALID_REGNUM
9297
          && current_function_uses_pic_offset_table)
9298
        save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9299
    }
9300
  else
9301
    {
9302
      /* In the normal case we only need to save those registers
9303
         which are call saved and which are used by this function.  */
9304
      for (reg = 0; reg <= 10; reg++)
9305
        if (regs_ever_live[reg] && ! call_used_regs [reg])
9306
          save_reg_mask |= (1 << reg);
9307
 
9308
      /* Handle the frame pointer as a special case.  */
9309
      if (! TARGET_APCS_FRAME
9310
          && ! frame_pointer_needed
9311
          && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
9312
          && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
9313
        save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9314
 
9315
      /* If we aren't loading the PIC register,
9316
         don't stack it even though it may be live.  */
9317
      if (flag_pic
9318
          && !TARGET_SINGLE_PIC_BASE
9319
          && arm_pic_register != INVALID_REGNUM
9320
          && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
9321
              || current_function_uses_pic_offset_table))
9322
        save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9323
    }
9324
 
9325
  /* Save registers so the exception handler can modify them.  */
9326
  if (current_function_calls_eh_return)
9327
    {
9328
      unsigned int i;
9329
 
9330
      for (i = 0; ; i++)
9331
        {
9332
          reg = EH_RETURN_DATA_REGNO (i);
9333
          if (reg == INVALID_REGNUM)
9334
            break;
9335
          save_reg_mask |= 1 << reg;
9336
        }
9337
    }
9338
 
9339
  return save_reg_mask;
9340
}
9341
 
9342
/* Compute a bit mask of which registers need to be
9343
   saved on the stack for the current function.  */
9344
 
9345
static unsigned long
9346
arm_compute_save_reg_mask (void)
9347
{
9348
  unsigned int save_reg_mask = 0;
9349
  unsigned long func_type = arm_current_func_type ();
9350
 
9351
  if (IS_NAKED (func_type))
9352
    /* This should never really happen.  */
9353
    return 0;
9354
 
9355
  /* If we are creating a stack frame, then we must save the frame pointer,
9356
     IP (which will hold the old stack pointer), LR and the PC.  */
9357
  if (frame_pointer_needed)
9358
    save_reg_mask |=
9359
      (1 << ARM_HARD_FRAME_POINTER_REGNUM)
9360
      | (1 << IP_REGNUM)
9361
      | (1 << LR_REGNUM)
9362
      | (1 << PC_REGNUM);
9363
 
9364
  /* Volatile functions do not return, so there
9365
     is no need to save any other registers.  */
9366
  if (IS_VOLATILE (func_type))
9367
    return save_reg_mask;
9368
 
9369
  save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
9370
 
9371
  /* Decide if we need to save the link register.
9372
     Interrupt routines have their own banked link register,
9373
     so they never need to save it.
9374
     Otherwise if we do not use the link register we do not need to save
9375
     it.  If we are pushing other registers onto the stack however, we
9376
     can save an instruction in the epilogue by pushing the link register
9377
     now and then popping it back into the PC.  This incurs extra memory
9378
     accesses though, so we only do it when optimizing for size, and only
9379
     if we know that we will not need a fancy return sequence.  */
9380
  if (regs_ever_live [LR_REGNUM]
9381
          || (save_reg_mask
9382
              && optimize_size
9383
              && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9384
              && !current_function_calls_eh_return))
9385
    save_reg_mask |= 1 << LR_REGNUM;
9386
 
9387
  if (cfun->machine->lr_save_eliminated)
9388
    save_reg_mask &= ~ (1 << LR_REGNUM);
9389
 
9390
  if (TARGET_REALLY_IWMMXT
9391
      && ((bit_count (save_reg_mask)
9392
           + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9393
    {
9394
      unsigned int reg;
9395
 
9396
      /* The total number of registers that are going to be pushed
9397
         onto the stack is odd.  We need to ensure that the stack
9398
         is 64-bit aligned before we start to save iWMMXt registers,
9399
         and also before we start to create locals.  (A local variable
9400
         might be a double or long long which we will load/store using
9401
         an iWMMXt instruction).  Therefore we need to push another
9402
         ARM register, so that the stack will be 64-bit aligned.  We
9403
         try to avoid using the arg registers (r0 -r3) as they might be
9404
         used to pass values in a tail call.  */
9405
      for (reg = 4; reg <= 12; reg++)
9406
        if ((save_reg_mask & (1 << reg)) == 0)
9407
          break;
9408
 
9409
      if (reg <= 12)
9410
        save_reg_mask |= (1 << reg);
9411
      else
9412
        {
9413
          cfun->machine->sibcall_blocked = 1;
9414
          save_reg_mask |= (1 << 3);
9415
        }
9416
    }
9417
 
9418
  return save_reg_mask;
9419
}
9420
 
9421
 
9422
/* Compute a bit mask of which registers need to be
9423
   saved on the stack for the current function.  */
9424
static unsigned long
9425
thumb_compute_save_reg_mask (void)
9426
{
9427
  unsigned long mask;
9428
  unsigned reg;
9429
 
9430
  mask = 0;
9431
  for (reg = 0; reg < 12; reg ++)
9432
    if (regs_ever_live[reg] && !call_used_regs[reg])
9433
      mask |= 1 << reg;
9434
 
9435
  if (flag_pic
9436
      && !TARGET_SINGLE_PIC_BASE
9437
      && arm_pic_register != INVALID_REGNUM
9438
      && current_function_uses_pic_offset_table)
9439
    mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9440
 
9441
  /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
9442
  if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
9443
    mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
9444
 
9445
  /* LR will also be pushed if any lo regs are pushed.  */
9446
  if (mask & 0xff || thumb_force_lr_save ())
9447
    mask |= (1 << LR_REGNUM);
9448
 
9449
  /* Make sure we have a low work register if we need one.
9450
     We will need one if we are going to push a high register,
9451
     but we are not currently intending to push a low register.  */
9452
  if ((mask & 0xff) == 0
9453
      && ((mask & 0x0f00) || TARGET_BACKTRACE))
9454
    {
9455
      /* Use thumb_find_work_register to choose which register
9456
         we will use.  If the register is live then we will
9457
         have to push it.  Use LAST_LO_REGNUM as our fallback
9458
         choice for the register to select.  */
9459
      reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9460
 
9461
      if (! call_used_regs[reg])
9462
        mask |= 1 << reg;
9463
    }
9464
 
9465
  return mask;
9466
}
9467
 
9468
 
9469
/* Return the number of bytes required to save VFP registers.  */
9470
static int
9471
arm_get_vfp_saved_size (void)
9472
{
9473
  unsigned int regno;
9474
  int count;
9475
  int saved;
9476
 
9477
  saved = 0;
9478
  /* Space for saved VFP registers.  */
9479
  if (TARGET_HARD_FLOAT && TARGET_VFP)
9480
    {
9481
      count = 0;
9482
      for (regno = FIRST_VFP_REGNUM;
9483
           regno < LAST_VFP_REGNUM;
9484
           regno += 2)
9485
        {
9486
          if ((!regs_ever_live[regno] || call_used_regs[regno])
9487
              && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9488
            {
9489
              if (count > 0)
9490
                {
9491
                  /* Workaround ARM10 VFPr1 bug.  */
9492
                  if (count == 2 && !arm_arch6)
9493
                    count++;
9494
                  saved += count * 8 + 4;
9495
                }
9496
              count = 0;
9497
            }
9498
          else
9499
            count++;
9500
        }
9501
      if (count > 0)
9502
        {
9503
          if (count == 2 && !arm_arch6)
9504
            count++;
9505
          saved += count * 8 + 4;
9506
        }
9507
    }
9508
  return saved;
9509
}
9510
 
9511
 
9512
/* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9513
   everything bar the final return instruction.  */
9514
const char *
9515
output_return_instruction (rtx operand, int really_return, int reverse)
9516
{
9517
  char conditional[10];
9518
  char instr[100];
9519
  unsigned reg;
9520
  unsigned long live_regs_mask;
9521
  unsigned long func_type;
9522
  arm_stack_offsets *offsets;
9523
 
9524
  func_type = arm_current_func_type ();
9525
 
9526
  if (IS_NAKED (func_type))
9527
    return "";
9528
 
9529
  if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9530
    {
9531
      /* If this function was declared non-returning, and we have
9532
         found a tail call, then we have to trust that the called
9533
         function won't return.  */
9534
      if (really_return)
9535
        {
9536
          rtx ops[2];
9537
 
9538
          /* Otherwise, trap an attempted return by aborting.  */
9539
          ops[0] = operand;
9540
          ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9541
                                       : "abort");
9542
          assemble_external_libcall (ops[1]);
9543
          output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9544
        }
9545
 
9546
      return "";
9547
    }
9548
 
9549
  gcc_assert (!current_function_calls_alloca || really_return);
9550
 
9551
  sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9552
 
9553
  return_used_this_function = 1;
9554
 
9555
  live_regs_mask = arm_compute_save_reg_mask ();
9556
 
9557
  if (live_regs_mask)
9558
    {
9559
      const char * return_reg;
9560
 
9561
      /* If we do not have any special requirements for function exit
9562
         (e.g. interworking, or ISR) then we can load the return address
9563
         directly into the PC.  Otherwise we must load it into LR.  */
9564
      if (really_return
9565
          && ! TARGET_INTERWORK)
9566
        return_reg = reg_names[PC_REGNUM];
9567
      else
9568
        return_reg = reg_names[LR_REGNUM];
9569
 
9570
      if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9571
        {
9572
          /* There are three possible reasons for the IP register
9573
             being saved.  1) a stack frame was created, in which case
9574
             IP contains the old stack pointer, or 2) an ISR routine
9575
             corrupted it, or 3) it was saved to align the stack on
9576
             iWMMXt.  In case 1, restore IP into SP, otherwise just
9577
             restore IP.  */
9578
          if (frame_pointer_needed)
9579
            {
9580
              live_regs_mask &= ~ (1 << IP_REGNUM);
9581
              live_regs_mask |=   (1 << SP_REGNUM);
9582
            }
9583
          else
9584
            gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9585
        }
9586
 
9587
      /* On some ARM architectures it is faster to use LDR rather than
9588
         LDM to load a single register.  On other architectures, the
9589
         cost is the same.  In 26 bit mode, or for exception handlers,
9590
         we have to use LDM to load the PC so that the CPSR is also
9591
         restored.  */
9592
      for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9593
        if (live_regs_mask == (1U << reg))
9594
          break;
9595
 
9596
      if (reg <= LAST_ARM_REGNUM
9597
          && (reg != LR_REGNUM
9598
              || ! really_return
9599
              || ! IS_INTERRUPT (func_type)))
9600
        {
9601
          sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9602
                   (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9603
        }
9604
      else
9605
        {
9606
          char *p;
9607
          int first = 1;
9608
 
9609
          /* Generate the load multiple instruction to restore the
9610
             registers.  Note we can get here, even if
9611
             frame_pointer_needed is true, but only if sp already
9612
             points to the base of the saved core registers.  */
9613
          if (live_regs_mask & (1 << SP_REGNUM))
9614
            {
9615
              unsigned HOST_WIDE_INT stack_adjust;
9616
 
9617
              offsets = arm_get_frame_offsets ();
9618
              stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9619
              gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9620
 
9621
              if (stack_adjust && arm_arch5)
9622
                sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9623
              else
9624
                {
9625
                  /* If we can't use ldmib (SA110 bug),
9626
                     then try to pop r3 instead.  */
9627
                  if (stack_adjust)
9628
                    live_regs_mask |= 1 << 3;
9629
                  sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9630
                }
9631
            }
9632
          else
9633
            sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9634
 
9635
          p = instr + strlen (instr);
9636
 
9637
          for (reg = 0; reg <= SP_REGNUM; reg++)
9638
            if (live_regs_mask & (1 << reg))
9639
              {
9640
                int l = strlen (reg_names[reg]);
9641
 
9642
                if (first)
9643
                  first = 0;
9644
                else
9645
                  {
9646
                    memcpy (p, ", ", 2);
9647
                    p += 2;
9648
                  }
9649
 
9650
                memcpy (p, "%|", 2);
9651
                memcpy (p + 2, reg_names[reg], l);
9652
                p += l + 2;
9653
              }
9654
 
9655
          if (live_regs_mask & (1 << LR_REGNUM))
9656
            {
9657
              sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9658
              /* If returning from an interrupt, restore the CPSR.  */
9659
              if (IS_INTERRUPT (func_type))
9660
                strcat (p, "^");
9661
            }
9662
          else
9663
            strcpy (p, "}");
9664
        }
9665
 
9666
      output_asm_insn (instr, & operand);
9667
 
9668
      /* See if we need to generate an extra instruction to
9669
         perform the actual function return.  */
9670
      if (really_return
9671
          && func_type != ARM_FT_INTERWORKED
9672
          && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9673
        {
9674
          /* The return has already been handled
9675
             by loading the LR into the PC.  */
9676
          really_return = 0;
9677
        }
9678
    }
9679
 
9680
  if (really_return)
9681
    {
9682
      switch ((int) ARM_FUNC_TYPE (func_type))
9683
        {
9684
        case ARM_FT_ISR:
9685
        case ARM_FT_FIQ:
9686
          sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9687
          break;
9688
 
9689
        case ARM_FT_INTERWORKED:
9690
          sprintf (instr, "bx%s\t%%|lr", conditional);
9691
          break;
9692
 
9693
        case ARM_FT_EXCEPTION:
9694
          sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9695
          break;
9696
 
9697
        default:
9698
          /* Use bx if it's available.  */
9699
          if (arm_arch5 || arm_arch4t)
9700
            sprintf (instr, "bx%s\t%%|lr", conditional);
9701
          else
9702
            sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9703
          break;
9704
        }
9705
 
9706
      output_asm_insn (instr, & operand);
9707
    }
9708
 
9709
  return "";
9710
}
9711
 
9712
/* Write the function name into the code section, directly preceding
9713
   the function prologue.
9714
 
9715
   Code will be output similar to this:
9716
     t0
9717
         .ascii "arm_poke_function_name", 0
9718
         .align
9719
     t1
9720
         .word 0xff000000 + (t1 - t0)
9721
     arm_poke_function_name
9722
         mov     ip, sp
9723
         stmfd   sp!, {fp, ip, lr, pc}
9724
         sub     fp, ip, #4
9725
 
9726
   When performing a stack backtrace, code can inspect the value
9727
   of 'pc' stored at 'fp' + 0.  If the trace function then looks
9728
   at location pc - 12 and the top 8 bits are set, then we know
9729
   that there is a function name embedded immediately preceding this
9730
   location and has length ((pc[-3]) & 0xff000000).
9731
 
9732
   We assume that pc is declared as a pointer to an unsigned long.
9733
 
9734
   It is of no benefit to output the function name if we are assembling
9735
   a leaf function.  These function types will not contain a stack
9736
   backtrace structure, therefore it is not possible to determine the
9737
   function name.  */
9738
void
9739
arm_poke_function_name (FILE *stream, const char *name)
9740
{
9741
  unsigned long alignlength;
9742
  unsigned long length;
9743
  rtx           x;
9744
 
9745
  length      = strlen (name) + 1;
9746
  alignlength = ROUND_UP_WORD (length);
9747
 
9748
  ASM_OUTPUT_ASCII (stream, name, length);
9749
  ASM_OUTPUT_ALIGN (stream, 2);
9750
  x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9751
  assemble_aligned_integer (UNITS_PER_WORD, x);
9752
}
9753
 
9754
/* Place some comments into the assembler stream
9755
   describing the current function.  */
9756
static void
9757
arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9758
{
9759
  unsigned long func_type;
9760
 
9761
  if (!TARGET_ARM)
9762
    {
9763
      thumb_output_function_prologue (f, frame_size);
9764
      return;
9765
    }
9766
 
9767
  /* Sanity check.  */
9768
  gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9769
 
9770
  func_type = arm_current_func_type ();
9771
 
9772
  switch ((int) ARM_FUNC_TYPE (func_type))
9773
    {
9774
    default:
9775
    case ARM_FT_NORMAL:
9776
      break;
9777
    case ARM_FT_INTERWORKED:
9778
      asm_fprintf (f, "\t%@ Function supports interworking.\n");
9779
      break;
9780
    case ARM_FT_ISR:
9781
      asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9782
      break;
9783
    case ARM_FT_FIQ:
9784
      asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9785
      break;
9786
    case ARM_FT_EXCEPTION:
9787
      asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9788
      break;
9789
    }
9790
 
9791
  if (IS_NAKED (func_type))
9792
    asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9793
 
9794
  if (IS_VOLATILE (func_type))
9795
    asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9796
 
9797
  if (IS_NESTED (func_type))
9798
    asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9799
 
9800
  asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9801
               current_function_args_size,
9802
               current_function_pretend_args_size, frame_size);
9803
 
9804
  asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9805
               frame_pointer_needed,
9806
               cfun->machine->uses_anonymous_args);
9807
 
9808
  if (cfun->machine->lr_save_eliminated)
9809
    asm_fprintf (f, "\t%@ link register save eliminated.\n");
9810
 
9811
  if (current_function_calls_eh_return)
9812
    asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9813
 
9814
#ifdef AOF_ASSEMBLER
9815
  if (flag_pic)
9816
    asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9817
#endif
9818
 
9819
  return_used_this_function = 0;
9820
}
9821
 
9822
const char *
9823
arm_output_epilogue (rtx sibling)
9824
{
9825
  int reg;
9826
  unsigned long saved_regs_mask;
9827
  unsigned long func_type;
9828
  /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9829
     frame that is $fp + 4 for a non-variadic function.  */
9830
  int floats_offset = 0;
9831
  rtx operands[3];
9832
  FILE * f = asm_out_file;
9833
  unsigned int lrm_count = 0;
9834
  int really_return = (sibling == NULL);
9835
  int start_reg;
9836
  arm_stack_offsets *offsets;
9837
 
9838
  /* If we have already generated the return instruction
9839
     then it is futile to generate anything else.  */
9840
  if (use_return_insn (FALSE, sibling) && return_used_this_function)
9841
    return "";
9842
 
9843
  func_type = arm_current_func_type ();
9844
 
9845
  if (IS_NAKED (func_type))
9846
    /* Naked functions don't have epilogues.  */
9847
    return "";
9848
 
9849
  if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9850
    {
9851
      rtx op;
9852
 
9853
      /* A volatile function should never return.  Call abort.  */
9854
      op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9855
      assemble_external_libcall (op);
9856
      output_asm_insn ("bl\t%a0", &op);
9857
 
9858
      return "";
9859
    }
9860
 
9861
  /* If we are throwing an exception, then we really must be doing a
9862
     return, so we can't tail-call.  */
9863
  gcc_assert (!current_function_calls_eh_return || really_return);
9864
 
9865
  offsets = arm_get_frame_offsets ();
9866
  saved_regs_mask = arm_compute_save_reg_mask ();
9867
 
9868
  if (TARGET_IWMMXT)
9869
    lrm_count = bit_count (saved_regs_mask);
9870
 
9871
  floats_offset = offsets->saved_args;
9872
  /* Compute how far away the floats will be.  */
9873
  for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9874
    if (saved_regs_mask & (1 << reg))
9875
      floats_offset += 4;
9876
 
9877
  if (frame_pointer_needed)
9878
    {
9879
      /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9880
      int vfp_offset = offsets->frame;
9881
 
9882
      if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9883
        {
9884
          for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9885
            if (regs_ever_live[reg] && !call_used_regs[reg])
9886
              {
9887
                floats_offset += 12;
9888
                asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9889
                             reg, FP_REGNUM, floats_offset - vfp_offset);
9890
              }
9891
        }
9892
      else
9893
        {
9894
          start_reg = LAST_FPA_REGNUM;
9895
 
9896
          for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9897
            {
9898
              if (regs_ever_live[reg] && !call_used_regs[reg])
9899
                {
9900
                  floats_offset += 12;
9901
 
9902
                  /* We can't unstack more than four registers at once.  */
9903
                  if (start_reg - reg == 3)
9904
                    {
9905
                      asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9906
                                   reg, FP_REGNUM, floats_offset - vfp_offset);
9907
                      start_reg = reg - 1;
9908
                    }
9909
                }
9910
              else
9911
                {
9912
                  if (reg != start_reg)
9913
                    asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9914
                                 reg + 1, start_reg - reg,
9915
                                 FP_REGNUM, floats_offset - vfp_offset);
9916
                  start_reg = reg - 1;
9917
                }
9918
            }
9919
 
9920
          /* Just in case the last register checked also needs unstacking.  */
9921
          if (reg != start_reg)
9922
            asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9923
                         reg + 1, start_reg - reg,
9924
                         FP_REGNUM, floats_offset - vfp_offset);
9925
        }
9926
 
9927
      if (TARGET_HARD_FLOAT && TARGET_VFP)
9928
        {
9929
          int saved_size;
9930
 
9931
          /* The fldmx insn does not have base+offset addressing modes,
9932
             so we use IP to hold the address.  */
9933
          saved_size = arm_get_vfp_saved_size ();
9934
 
9935
          if (saved_size > 0)
9936
            {
9937
              floats_offset += saved_size;
9938
              asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9939
                           FP_REGNUM, floats_offset - vfp_offset);
9940
            }
9941
          start_reg = FIRST_VFP_REGNUM;
9942
          for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9943
            {
9944
              if ((!regs_ever_live[reg] || call_used_regs[reg])
9945
                  && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9946
                {
9947
                  if (start_reg != reg)
9948
                    arm_output_fldmx (f, IP_REGNUM,
9949
                                      (start_reg - FIRST_VFP_REGNUM) / 2,
9950
                                      (reg - start_reg) / 2);
9951
                  start_reg = reg + 2;
9952
                }
9953
            }
9954
          if (start_reg != reg)
9955
            arm_output_fldmx (f, IP_REGNUM,
9956
                              (start_reg - FIRST_VFP_REGNUM) / 2,
9957
                              (reg - start_reg) / 2);
9958
        }
9959
 
9960
      if (TARGET_IWMMXT)
9961
        {
9962
          /* The frame pointer is guaranteed to be non-double-word aligned.
9963
             This is because it is set to (old_stack_pointer - 4) and the
9964
             old_stack_pointer was double word aligned.  Thus the offset to
9965
             the iWMMXt registers to be loaded must also be non-double-word
9966
             sized, so that the resultant address *is* double-word aligned.
9967
             We can ignore floats_offset since that was already included in
9968
             the live_regs_mask.  */
9969
          lrm_count += (lrm_count % 2 ? 2 : 1);
9970
 
9971
          for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9972
            if (regs_ever_live[reg] && !call_used_regs[reg])
9973
              {
9974
                asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9975
                             reg, FP_REGNUM, lrm_count * 4);
9976
                lrm_count += 2;
9977
              }
9978
        }
9979
 
9980
      /* saved_regs_mask should contain the IP, which at the time of stack
9981
         frame generation actually contains the old stack pointer.  So a
9982
         quick way to unwind the stack is just pop the IP register directly
9983
         into the stack pointer.  */
9984
      gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9985
      saved_regs_mask &= ~ (1 << IP_REGNUM);
9986
      saved_regs_mask |=   (1 << SP_REGNUM);
9987
 
9988
      /* There are two registers left in saved_regs_mask - LR and PC.  We
9989
         only need to restore the LR register (the return address), but to
9990
         save time we can load it directly into the PC, unless we need a
9991
         special function exit sequence, or we are not really returning.  */
9992
      if (really_return
9993
          && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9994
          && !current_function_calls_eh_return)
9995
        /* Delete the LR from the register mask, so that the LR on
9996
           the stack is loaded into the PC in the register mask.  */
9997
        saved_regs_mask &= ~ (1 << LR_REGNUM);
9998
      else
9999
        saved_regs_mask &= ~ (1 << PC_REGNUM);
10000
 
10001
      /* We must use SP as the base register, because SP is one of the
10002
         registers being restored.  If an interrupt or page fault
10003
         happens in the ldm instruction, the SP might or might not
10004
         have been restored.  That would be bad, as then SP will no
10005
         longer indicate the safe area of stack, and we can get stack
10006
         corruption.  Using SP as the base register means that it will
10007
         be reset correctly to the original value, should an interrupt
10008
         occur.  If the stack pointer already points at the right
10009
         place, then omit the subtraction.  */
10010
      if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
10011
          || current_function_calls_alloca)
10012
        asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
10013
                     4 * bit_count (saved_regs_mask));
10014
      print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
10015
 
10016
      if (IS_INTERRUPT (func_type))
10017
        /* Interrupt handlers will have pushed the
10018
           IP onto the stack, so restore it now.  */
10019
        print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
10020
    }
10021
  else
10022
    {
10023
      /* Restore stack pointer if necessary.  */
10024
      if (offsets->outgoing_args != offsets->saved_regs)
10025
        {
10026
          operands[0] = operands[1] = stack_pointer_rtx;
10027
          operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
10028
          output_add_immediate (operands);
10029
        }
10030
 
10031
      if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10032
        {
10033
          for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10034
            if (regs_ever_live[reg] && !call_used_regs[reg])
10035
              asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
10036
                           reg, SP_REGNUM);
10037
        }
10038
      else
10039
        {
10040
          start_reg = FIRST_FPA_REGNUM;
10041
 
10042
          for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10043
            {
10044
              if (regs_ever_live[reg] && !call_used_regs[reg])
10045
                {
10046
                  if (reg - start_reg == 3)
10047
                    {
10048
                      asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
10049
                                   start_reg, SP_REGNUM);
10050
                      start_reg = reg + 1;
10051
                    }
10052
                }
10053
              else
10054
                {
10055
                  if (reg != start_reg)
10056
                    asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10057
                                 start_reg, reg - start_reg,
10058
                                 SP_REGNUM);
10059
 
10060
                  start_reg = reg + 1;
10061
                }
10062
            }
10063
 
10064
          /* Just in case the last register checked also needs unstacking.  */
10065
          if (reg != start_reg)
10066
            asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10067
                         start_reg, reg - start_reg, SP_REGNUM);
10068
        }
10069
 
10070
      if (TARGET_HARD_FLOAT && TARGET_VFP)
10071
        {
10072
          start_reg = FIRST_VFP_REGNUM;
10073
          for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10074
            {
10075
              if ((!regs_ever_live[reg] || call_used_regs[reg])
10076
                  && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10077
                {
10078
                  if (start_reg != reg)
10079
                    arm_output_fldmx (f, SP_REGNUM,
10080
                                      (start_reg - FIRST_VFP_REGNUM) / 2,
10081
                                      (reg - start_reg) / 2);
10082
                  start_reg = reg + 2;
10083
                }
10084
            }
10085
          if (start_reg != reg)
10086
            arm_output_fldmx (f, SP_REGNUM,
10087
                              (start_reg - FIRST_VFP_REGNUM) / 2,
10088
                              (reg - start_reg) / 2);
10089
        }
10090
      if (TARGET_IWMMXT)
10091
        for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10092
          if (regs_ever_live[reg] && !call_used_regs[reg])
10093
            asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
10094
 
10095
      /* If we can, restore the LR into the PC.  */
10096
      if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10097
          && really_return
10098
          && current_function_pretend_args_size == 0
10099
          && saved_regs_mask & (1 << LR_REGNUM)
10100
          && !current_function_calls_eh_return)
10101
        {
10102
          saved_regs_mask &= ~ (1 << LR_REGNUM);
10103
          saved_regs_mask |=   (1 << PC_REGNUM);
10104
        }
10105
 
10106
      /* Load the registers off the stack.  If we only have one register
10107
         to load use the LDR instruction - it is faster.  */
10108
      if (saved_regs_mask == (1 << LR_REGNUM))
10109
        {
10110
          asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
10111
        }
10112
      else if (saved_regs_mask)
10113
        {
10114
          if (saved_regs_mask & (1 << SP_REGNUM))
10115
            /* Note - write back to the stack register is not enabled
10116
               (i.e. "ldmfd sp!...").  We know that the stack pointer is
10117
               in the list of registers and if we add writeback the
10118
               instruction becomes UNPREDICTABLE.  */
10119
            print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
10120
          else
10121
            print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
10122
        }
10123
 
10124
      if (current_function_pretend_args_size)
10125
        {
10126
          /* Unwind the pre-pushed regs.  */
10127
          operands[0] = operands[1] = stack_pointer_rtx;
10128
          operands[2] = GEN_INT (current_function_pretend_args_size);
10129
          output_add_immediate (operands);
10130
        }
10131
    }
10132
 
10133
  /* We may have already restored PC directly from the stack.  */
10134
  if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10135
    return "";
10136
 
10137
  /* Stack adjustment for exception handler.  */
10138
  if (current_function_calls_eh_return)
10139
    asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10140
                 ARM_EH_STACKADJ_REGNUM);
10141
 
10142
  /* Generate the return instruction.  */
10143
  switch ((int) ARM_FUNC_TYPE (func_type))
10144
    {
10145
    case ARM_FT_ISR:
10146
    case ARM_FT_FIQ:
10147
      asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10148
      break;
10149
 
10150
    case ARM_FT_EXCEPTION:
10151
      asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10152
      break;
10153
 
10154
    case ARM_FT_INTERWORKED:
10155
      asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10156
      break;
10157
 
10158
    default:
10159
      if (arm_arch5 || arm_arch4t)
10160
        asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10161
      else
10162
        asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10163
      break;
10164
    }
10165
 
10166
  return "";
10167
}
10168
 
10169
static void
10170
arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10171
                              HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10172
{
10173
  arm_stack_offsets *offsets;
10174
 
10175
  if (TARGET_THUMB)
10176
    {
10177
      int regno;
10178
 
10179
      /* Emit any call-via-reg trampolines that are needed for v4t support
10180
         of call_reg and call_value_reg type insns.  */
10181
      for (regno = 0; regno < LR_REGNUM; regno++)
10182
        {
10183
          rtx label = cfun->machine->call_via[regno];
10184
 
10185
          if (label != NULL)
10186
            {
10187
              switch_to_section (function_section (current_function_decl));
10188
              targetm.asm_out.internal_label (asm_out_file, "L",
10189
                                              CODE_LABEL_NUMBER (label));
10190
              asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10191
            }
10192
        }
10193
 
10194
      /* ??? Probably not safe to set this here, since it assumes that a
10195
         function will be emitted as assembly immediately after we generate
10196
         RTL for it.  This does not happen for inline functions.  */
10197
      return_used_this_function = 0;
10198
    }
10199
  else
10200
    {
10201
      /* We need to take into account any stack-frame rounding.  */
10202
      offsets = arm_get_frame_offsets ();
10203
 
10204
      gcc_assert (!use_return_insn (FALSE, NULL)
10205
                  || !return_used_this_function
10206
                  || offsets->saved_regs == offsets->outgoing_args
10207
                  || frame_pointer_needed);
10208
 
10209
      /* Reset the ARM-specific per-function variables.  */
10210
      after_arm_reorg = 0;
10211
    }
10212
}
10213
 
10214
/* Generate and emit an insn that we will recognize as a push_multi.
10215
   Unfortunately, since this insn does not reflect very well the actual
10216
   semantics of the operation, we need to annotate the insn for the benefit
10217
   of DWARF2 frame unwind information.  */
10218
static rtx
10219
emit_multi_reg_push (unsigned long mask)
10220
{
10221
  int num_regs = 0;
10222
  int num_dwarf_regs;
10223
  int i, j;
10224
  rtx par;
10225
  rtx dwarf;
10226
  int dwarf_par_index;
10227
  rtx tmp, reg;
10228
 
10229
  for (i = 0; i <= LAST_ARM_REGNUM; i++)
10230
    if (mask & (1 << i))
10231
      num_regs++;
10232
 
10233
  gcc_assert (num_regs && num_regs <= 16);
10234
 
10235
  /* We don't record the PC in the dwarf frame information.  */
10236
  num_dwarf_regs = num_regs;
10237
  if (mask & (1 << PC_REGNUM))
10238
    num_dwarf_regs--;
10239
 
10240
  /* For the body of the insn we are going to generate an UNSPEC in
10241
     parallel with several USEs.  This allows the insn to be recognized
10242
     by the push_multi pattern in the arm.md file.  The insn looks
10243
     something like this:
10244
 
10245
       (parallel [
10246
           (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
10247
                (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
10248
           (use (reg:SI 11 fp))
10249
           (use (reg:SI 12 ip))
10250
           (use (reg:SI 14 lr))
10251
           (use (reg:SI 15 pc))
10252
        ])
10253
 
10254
     For the frame note however, we try to be more explicit and actually
10255
     show each register being stored into the stack frame, plus a (single)
10256
     decrement of the stack pointer.  We do it this way in order to be
10257
     friendly to the stack unwinding code, which only wants to see a single
10258
     stack decrement per instruction.  The RTL we generate for the note looks
10259
     something like this:
10260
 
10261
      (sequence [
10262
           (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
10263
           (set (mem:SI (reg:SI sp)) (reg:SI r4))
10264
           (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
10265
           (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
10266
           (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
10267
        ])
10268
 
10269
      This sequence is used both by the code to support stack unwinding for
10270
      exceptions handlers and the code to generate dwarf2 frame debugging.  */
10271
 
10272
  par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
10273
  dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
10274
  dwarf_par_index = 1;
10275
 
10276
  for (i = 0; i <= LAST_ARM_REGNUM; i++)
10277
    {
10278
      if (mask & (1 << i))
10279
        {
10280
          reg = gen_rtx_REG (SImode, i);
10281
 
10282
          XVECEXP (par, 0, 0)
10283
            = gen_rtx_SET (VOIDmode,
10284
                           gen_frame_mem (BLKmode,
10285
                                          gen_rtx_PRE_DEC (BLKmode,
10286
                                                           stack_pointer_rtx)),
10287
                           gen_rtx_UNSPEC (BLKmode,
10288
                                           gen_rtvec (1, reg),
10289
                                           UNSPEC_PUSH_MULT));
10290
 
10291
          if (i != PC_REGNUM)
10292
            {
10293
              tmp = gen_rtx_SET (VOIDmode,
10294
                                 gen_frame_mem (SImode, stack_pointer_rtx),
10295
                                 reg);
10296
              RTX_FRAME_RELATED_P (tmp) = 1;
10297
              XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
10298
              dwarf_par_index++;
10299
            }
10300
 
10301
          break;
10302
        }
10303
    }
10304
 
10305
  for (j = 1, i++; j < num_regs; i++)
10306
    {
10307
      if (mask & (1 << i))
10308
        {
10309
          reg = gen_rtx_REG (SImode, i);
10310
 
10311
          XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
10312
 
10313
          if (i != PC_REGNUM)
10314
            {
10315
              tmp
10316
                = gen_rtx_SET (VOIDmode,
10317
                               gen_frame_mem (SImode,
10318
                                              plus_constant (stack_pointer_rtx,
10319
                                                             4 * j)),
10320
                               reg);
10321
              RTX_FRAME_RELATED_P (tmp) = 1;
10322
              XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
10323
            }
10324
 
10325
          j++;
10326
        }
10327
    }
10328
 
10329
  par = emit_insn (par);
10330
 
10331
  tmp = gen_rtx_SET (VOIDmode,
10332
                     stack_pointer_rtx,
10333
                     plus_constant (stack_pointer_rtx, -4 * num_regs));
10334
  RTX_FRAME_RELATED_P (tmp) = 1;
10335
  XVECEXP (dwarf, 0, 0) = tmp;
10336
 
10337
  REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10338
                                       REG_NOTES (par));
10339
  return par;
10340
}
10341
 
10342
/* Calculate the size of the return value that is passed in registers.  */
10343
static int
10344
arm_size_return_regs (void)
10345
{
10346
  enum machine_mode mode;
10347
 
10348
  if (current_function_return_rtx != 0)
10349
    mode = GET_MODE (current_function_return_rtx);
10350
  else
10351
    mode = DECL_MODE (DECL_RESULT (current_function_decl));
10352
 
10353
  return GET_MODE_SIZE (mode);
10354
}
10355
 
10356
static rtx
10357
emit_sfm (int base_reg, int count)
10358
{
10359
  rtx par;
10360
  rtx dwarf;
10361
  rtx tmp, reg;
10362
  int i;
10363
 
10364
  par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10365
  dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10366
 
10367
  reg = gen_rtx_REG (XFmode, base_reg++);
10368
 
10369
  XVECEXP (par, 0, 0)
10370
    = gen_rtx_SET (VOIDmode,
10371
                   gen_frame_mem (BLKmode,
10372
                                  gen_rtx_PRE_DEC (BLKmode,
10373
                                                   stack_pointer_rtx)),
10374
                   gen_rtx_UNSPEC (BLKmode,
10375
                                   gen_rtvec (1, reg),
10376
                                   UNSPEC_PUSH_MULT));
10377
  tmp = gen_rtx_SET (VOIDmode,
10378
                     gen_frame_mem (XFmode, stack_pointer_rtx), reg);
10379
  RTX_FRAME_RELATED_P (tmp) = 1;
10380
  XVECEXP (dwarf, 0, 1) = tmp;
10381
 
10382
  for (i = 1; i < count; i++)
10383
    {
10384
      reg = gen_rtx_REG (XFmode, base_reg++);
10385
      XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10386
 
10387
      tmp = gen_rtx_SET (VOIDmode,
10388
                         gen_frame_mem (XFmode,
10389
                                        plus_constant (stack_pointer_rtx,
10390
                                                       i * 12)),
10391
                         reg);
10392
      RTX_FRAME_RELATED_P (tmp) = 1;
10393
      XVECEXP (dwarf, 0, i + 1) = tmp;
10394
    }
10395
 
10396
  tmp = gen_rtx_SET (VOIDmode,
10397
                     stack_pointer_rtx,
10398
                     plus_constant (stack_pointer_rtx, -12 * count));
10399
 
10400
  RTX_FRAME_RELATED_P (tmp) = 1;
10401
  XVECEXP (dwarf, 0, 0) = tmp;
10402
 
10403
  par = emit_insn (par);
10404
  REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10405
                                       REG_NOTES (par));
10406
  return par;
10407
}
10408
 
10409
 
10410
/* Return true if the current function needs to save/restore LR.  */
10411
 
10412
static bool
10413
thumb_force_lr_save (void)
10414
{
10415
  return !cfun->machine->lr_save_eliminated
10416
         && (!leaf_function_p ()
10417
             || thumb_far_jump_used_p ()
10418
             || regs_ever_live [LR_REGNUM]);
10419
}
10420
 
10421
 
10422
/* Compute the distance from register FROM to register TO.
10423
   These can be the arg pointer (26), the soft frame pointer (25),
10424
   the stack pointer (13) or the hard frame pointer (11).
10425
   In thumb mode r7 is used as the soft frame pointer, if needed.
10426
   Typical stack layout looks like this:
10427
 
10428
       old stack pointer -> |    |
10429
                             ----
10430
                            |    | \
10431
                            |    |   saved arguments for
10432
                            |    |   vararg functions
10433
                            |    | /
10434
                              --
10435
   hard FP & arg pointer -> |    | \
10436
                            |    |   stack
10437
                            |    |   frame
10438
                            |    | /
10439
                              --
10440
                            |    | \
10441
                            |    |   call saved
10442
                            |    |   registers
10443
      soft frame pointer -> |    | /
10444
                              --
10445
                            |    | \
10446
                            |    |   local
10447
                            |    |   variables
10448
     locals base pointer -> |    | /
10449
                              --
10450
                            |    | \
10451
                            |    |   outgoing
10452
                            |    |   arguments
10453
   current stack pointer -> |    | /
10454
                              --
10455
 
10456
  For a given function some or all of these stack components
10457
  may not be needed, giving rise to the possibility of
10458
  eliminating some of the registers.
10459
 
10460
  The values returned by this function must reflect the behavior
10461
  of arm_expand_prologue() and arm_compute_save_reg_mask().
10462
 
10463
  The sign of the number returned reflects the direction of stack
10464
  growth, so the values are positive for all eliminations except
10465
  from the soft frame pointer to the hard frame pointer.
10466
 
10467
  SFP may point just inside the local variables block to ensure correct
10468
  alignment.  */
10469
 
10470
 
10471
/* Calculate stack offsets.  These are used to calculate register elimination
10472
   offsets and in prologue/epilogue code.  */
10473
 
10474
static arm_stack_offsets *
10475
arm_get_frame_offsets (void)
10476
{
10477
  struct arm_stack_offsets *offsets;
10478
  unsigned long func_type;
10479
  int leaf;
10480
  int saved;
10481
  HOST_WIDE_INT frame_size;
10482
 
10483
  offsets = &cfun->machine->stack_offsets;
10484
 
10485
  /* We need to know if we are a leaf function.  Unfortunately, it
10486
     is possible to be called after start_sequence has been called,
10487
     which causes get_insns to return the insns for the sequence,
10488
     not the function, which will cause leaf_function_p to return
10489
     the incorrect result.
10490
 
10491
     to know about leaf functions once reload has completed, and the
10492
     frame size cannot be changed after that time, so we can safely
10493
     use the cached value.  */
10494
 
10495
  if (reload_completed)
10496
    return offsets;
10497
 
10498
  /* Initially this is the size of the local variables.  It will translated
10499
     into an offset once we have determined the size of preceding data.  */
10500
  frame_size = ROUND_UP_WORD (get_frame_size ());
10501
 
10502
  leaf = leaf_function_p ();
10503
 
10504
  /* Space for variadic functions.  */
10505
  offsets->saved_args = current_function_pretend_args_size;
10506
 
10507
  offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10508
 
10509
  if (TARGET_ARM)
10510
    {
10511
      unsigned int regno;
10512
 
10513
      saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10514
 
10515
      /* We know that SP will be doubleword aligned on entry, and we must
10516
         preserve that condition at any subroutine call.  We also require the
10517
         soft frame pointer to be doubleword aligned.  */
10518
 
10519
      if (TARGET_REALLY_IWMMXT)
10520
        {
10521
          /* Check for the call-saved iWMMXt registers.  */
10522
          for (regno = FIRST_IWMMXT_REGNUM;
10523
               regno <= LAST_IWMMXT_REGNUM;
10524
               regno++)
10525
            if (regs_ever_live [regno] && ! call_used_regs [regno])
10526
              saved += 8;
10527
        }
10528
 
10529
      func_type = arm_current_func_type ();
10530
      if (! IS_VOLATILE (func_type))
10531
        {
10532
          /* Space for saved FPA registers.  */
10533
          for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10534
          if (regs_ever_live[regno] && ! call_used_regs[regno])
10535
            saved += 12;
10536
 
10537
          /* Space for saved VFP registers.  */
10538
          if (TARGET_HARD_FLOAT && TARGET_VFP)
10539
            saved += arm_get_vfp_saved_size ();
10540
        }
10541
    }
10542
  else /* TARGET_THUMB */
10543
    {
10544
      saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
10545
      if (TARGET_BACKTRACE)
10546
        saved += 16;
10547
    }
10548
 
10549
  /* Saved registers include the stack frame.  */
10550
  offsets->saved_regs = offsets->saved_args + saved;
10551
  offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10552
  /* A leaf function does not need any stack alignment if it has nothing
10553
     on the stack.  */
10554
  if (leaf && frame_size == 0)
10555
    {
10556
      offsets->outgoing_args = offsets->soft_frame;
10557
      offsets->locals_base = offsets->soft_frame;
10558
      return offsets;
10559
    }
10560
 
10561
  /* Ensure SFP has the correct alignment.  */
10562
  if (ARM_DOUBLEWORD_ALIGN
10563
      && (offsets->soft_frame & 7))
10564
    offsets->soft_frame += 4;
10565
 
10566
  offsets->locals_base = offsets->soft_frame + frame_size;
10567
  offsets->outgoing_args = (offsets->locals_base
10568
                            + current_function_outgoing_args_size);
10569
 
10570
  if (ARM_DOUBLEWORD_ALIGN)
10571
    {
10572
      /* Ensure SP remains doubleword aligned.  */
10573
      if (offsets->outgoing_args & 7)
10574
        offsets->outgoing_args += 4;
10575
      gcc_assert (!(offsets->outgoing_args & 7));
10576
    }
10577
 
10578
  return offsets;
10579
}
10580
 
10581
 
10582
/* Calculate the relative offsets for the different stack pointers.  Positive
10583
   offsets are in the direction of stack growth.  */
10584
 
10585
HOST_WIDE_INT
10586
arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10587
{
10588
  arm_stack_offsets *offsets;
10589
 
10590
  offsets = arm_get_frame_offsets ();
10591
 
10592
  /* OK, now we have enough information to compute the distances.
10593
     There must be an entry in these switch tables for each pair
10594
     of registers in ELIMINABLE_REGS, even if some of the entries
10595
     seem to be redundant or useless.  */
10596
  switch (from)
10597
    {
10598
    case ARG_POINTER_REGNUM:
10599
      switch (to)
10600
        {
10601
        case THUMB_HARD_FRAME_POINTER_REGNUM:
10602
          return 0;
10603
 
10604
        case FRAME_POINTER_REGNUM:
10605
          /* This is the reverse of the soft frame pointer
10606
             to hard frame pointer elimination below.  */
10607
          return offsets->soft_frame - offsets->saved_args;
10608
 
10609
        case ARM_HARD_FRAME_POINTER_REGNUM:
10610
          /* If there is no stack frame then the hard
10611
             frame pointer and the arg pointer coincide.  */
10612
          if (offsets->frame == offsets->saved_regs)
10613
            return 0;
10614
          /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10615
          return (frame_pointer_needed
10616
                  && cfun->static_chain_decl != NULL
10617
                  && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10618
 
10619
        case STACK_POINTER_REGNUM:
10620
          /* If nothing has been pushed on the stack at all
10621
             then this will return -4.  This *is* correct!  */
10622
          return offsets->outgoing_args - (offsets->saved_args + 4);
10623
 
10624
        default:
10625
          gcc_unreachable ();
10626
        }
10627
      gcc_unreachable ();
10628
 
10629
    case FRAME_POINTER_REGNUM:
10630
      switch (to)
10631
        {
10632
        case THUMB_HARD_FRAME_POINTER_REGNUM:
10633
          return 0;
10634
 
10635
        case ARM_HARD_FRAME_POINTER_REGNUM:
10636
          /* The hard frame pointer points to the top entry in the
10637
             stack frame.  The soft frame pointer to the bottom entry
10638
             in the stack frame.  If there is no stack frame at all,
10639
             then they are identical.  */
10640
 
10641
          return offsets->frame - offsets->soft_frame;
10642
 
10643
        case STACK_POINTER_REGNUM:
10644
          return offsets->outgoing_args - offsets->soft_frame;
10645
 
10646
        default:
10647
          gcc_unreachable ();
10648
        }
10649
      gcc_unreachable ();
10650
 
10651
    default:
10652
      /* You cannot eliminate from the stack pointer.
10653
         In theory you could eliminate from the hard frame
10654
         pointer to the stack pointer, but this will never
10655
         happen, since if a stack frame is not needed the
10656
         hard frame pointer will never be used.  */
10657
      gcc_unreachable ();
10658
    }
10659
}
10660
 
10661
 
10662
/* Generate the prologue instructions for entry into an ARM function.  */
10663
void
10664
arm_expand_prologue (void)
10665
{
10666
  int reg;
10667
  rtx amount;
10668
  rtx insn;
10669
  rtx ip_rtx;
10670
  unsigned long live_regs_mask;
10671
  unsigned long func_type;
10672
  int fp_offset = 0;
10673
  int saved_pretend_args = 0;
10674
  int saved_regs = 0;
10675
  unsigned HOST_WIDE_INT args_to_push;
10676
  arm_stack_offsets *offsets;
10677
 
10678
  func_type = arm_current_func_type ();
10679
 
10680
  /* Naked functions don't have prologues.  */
10681
  if (IS_NAKED (func_type))
10682
    return;
10683
 
10684
  /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10685
  args_to_push = current_function_pretend_args_size;
10686
 
10687
  /* Compute which register we will have to save onto the stack.  */
10688
  live_regs_mask = arm_compute_save_reg_mask ();
10689
 
10690
  ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10691
 
10692
  if (frame_pointer_needed)
10693
    {
10694
      if (IS_INTERRUPT (func_type))
10695
        {
10696
          /* Interrupt functions must not corrupt any registers.
10697
             Creating a frame pointer however, corrupts the IP
10698
             register, so we must push it first.  */
10699
          insn = emit_multi_reg_push (1 << IP_REGNUM);
10700
 
10701
          /* Do not set RTX_FRAME_RELATED_P on this insn.
10702
             The dwarf stack unwinding code only wants to see one
10703
             stack decrement per function, and this is not it.  If
10704
             this instruction is labeled as being part of the frame
10705
             creation sequence then dwarf2out_frame_debug_expr will
10706
             die when it encounters the assignment of IP to FP
10707
             later on, since the use of SP here establishes SP as
10708
             the CFA register and not IP.
10709
 
10710
             Anyway this instruction is not really part of the stack
10711
             frame creation although it is part of the prologue.  */
10712
        }
10713
      else if (IS_NESTED (func_type))
10714
        {
10715
          /* The Static chain register is the same as the IP register
10716
             used as a scratch register during stack frame creation.
10717
             To get around this need to find somewhere to store IP
10718
             whilst the frame is being created.  We try the following
10719
             places in order:
10720
 
10721
               1. The last argument register.
10722
               2. A slot on the stack above the frame.  (This only
10723
                  works if the function is not a varargs function).
10724
               3. Register r3, after pushing the argument registers
10725
                  onto the stack.
10726
 
10727
             Note - we only need to tell the dwarf2 backend about the SP
10728
             adjustment in the second variant; the static chain register
10729
             doesn't need to be unwound, as it doesn't contain a value
10730
             inherited from the caller.  */
10731
 
10732
          if (regs_ever_live[3] == 0)
10733
            insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10734
          else if (args_to_push == 0)
10735
            {
10736
              rtx dwarf;
10737
 
10738
              insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10739
              insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
10740
              fp_offset = 4;
10741
 
10742
              /* Just tell the dwarf backend that we adjusted SP.  */
10743
              dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10744
                                   plus_constant (stack_pointer_rtx,
10745
                                                  -fp_offset));
10746
              RTX_FRAME_RELATED_P (insn) = 1;
10747
              REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10748
                                                    dwarf, REG_NOTES (insn));
10749
            }
10750
          else
10751
            {
10752
              /* Store the args on the stack.  */
10753
              if (cfun->machine->uses_anonymous_args)
10754
                insn = emit_multi_reg_push
10755
                  ((0xf0 >> (args_to_push / 4)) & 0xf);
10756
              else
10757
                insn = emit_insn
10758
                  (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10759
                               GEN_INT (- args_to_push)));
10760
 
10761
              RTX_FRAME_RELATED_P (insn) = 1;
10762
 
10763
              saved_pretend_args = 1;
10764
              fp_offset = args_to_push;
10765
              args_to_push = 0;
10766
 
10767
              /* Now reuse r3 to preserve IP.  */
10768
              emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10769
            }
10770
        }
10771
 
10772
      insn = emit_set_insn (ip_rtx,
10773
                            plus_constant (stack_pointer_rtx, fp_offset));
10774
      RTX_FRAME_RELATED_P (insn) = 1;
10775
    }
10776
 
10777
  if (args_to_push)
10778
    {
10779
      /* Push the argument registers, or reserve space for them.  */
10780
      if (cfun->machine->uses_anonymous_args)
10781
        insn = emit_multi_reg_push
10782
          ((0xf0 >> (args_to_push / 4)) & 0xf);
10783
      else
10784
        insn = emit_insn
10785
          (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10786
                       GEN_INT (- args_to_push)));
10787
      RTX_FRAME_RELATED_P (insn) = 1;
10788
    }
10789
 
10790
  /* If this is an interrupt service routine, and the link register
10791
     is going to be pushed, and we are not creating a stack frame,
10792
     (which would involve an extra push of IP and a pop in the epilogue)
10793
     subtracting four from LR now will mean that the function return
10794
     can be done with a single instruction.  */
10795
  if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10796
      && (live_regs_mask & (1 << LR_REGNUM)) != 0
10797
      && ! frame_pointer_needed)
10798
    {
10799
      rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
10800
 
10801
      emit_set_insn (lr, plus_constant (lr, -4));
10802
    }
10803
 
10804
  if (live_regs_mask)
10805
    {
10806
      insn = emit_multi_reg_push (live_regs_mask);
10807
      saved_regs += bit_count (live_regs_mask) * 4;
10808
      RTX_FRAME_RELATED_P (insn) = 1;
10809
    }
10810
 
10811
  if (TARGET_IWMMXT)
10812
    for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10813
      if (regs_ever_live[reg] && ! call_used_regs [reg])
10814
        {
10815
          insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10816
          insn = gen_frame_mem (V2SImode, insn);
10817
          insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
10818
          RTX_FRAME_RELATED_P (insn) = 1;
10819
          saved_regs += 8;
10820
        }
10821
 
10822
  if (! IS_VOLATILE (func_type))
10823
    {
10824
      int start_reg;
10825
 
10826
      /* Save any floating point call-saved registers used by this
10827
         function.  */
10828
      if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10829
        {
10830
          for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10831
            if (regs_ever_live[reg] && !call_used_regs[reg])
10832
              {
10833
                insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10834
                insn = gen_frame_mem (XFmode, insn);
10835
                insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
10836
                RTX_FRAME_RELATED_P (insn) = 1;
10837
                saved_regs += 12;
10838
              }
10839
        }
10840
      else
10841
        {
10842
          start_reg = LAST_FPA_REGNUM;
10843
 
10844
          for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10845
            {
10846
              if (regs_ever_live[reg] && !call_used_regs[reg])
10847
                {
10848
                  if (start_reg - reg == 3)
10849
                    {
10850
                      insn = emit_sfm (reg, 4);
10851
                      RTX_FRAME_RELATED_P (insn) = 1;
10852
                      saved_regs += 48;
10853
                      start_reg = reg - 1;
10854
                    }
10855
                }
10856
              else
10857
                {
10858
                  if (start_reg != reg)
10859
                    {
10860
                      insn = emit_sfm (reg + 1, start_reg - reg);
10861
                      RTX_FRAME_RELATED_P (insn) = 1;
10862
                      saved_regs += (start_reg - reg) * 12;
10863
                    }
10864
                  start_reg = reg - 1;
10865
                }
10866
            }
10867
 
10868
          if (start_reg != reg)
10869
            {
10870
              insn = emit_sfm (reg + 1, start_reg - reg);
10871
              saved_regs += (start_reg - reg) * 12;
10872
              RTX_FRAME_RELATED_P (insn) = 1;
10873
            }
10874
        }
10875
      if (TARGET_HARD_FLOAT && TARGET_VFP)
10876
        {
10877
          start_reg = FIRST_VFP_REGNUM;
10878
 
10879
          for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10880
            {
10881
              if ((!regs_ever_live[reg] || call_used_regs[reg])
10882
                  && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10883
                {
10884
                  if (start_reg != reg)
10885
                    saved_regs += vfp_emit_fstmx (start_reg,
10886
                                                  (reg - start_reg) / 2);
10887
                  start_reg = reg + 2;
10888
                }
10889
            }
10890
          if (start_reg != reg)
10891
            saved_regs += vfp_emit_fstmx (start_reg,
10892
                                          (reg - start_reg) / 2);
10893
        }
10894
    }
10895
 
10896
  if (frame_pointer_needed)
10897
    {
10898
      /* Create the new frame pointer.  */
10899
      insn = GEN_INT (-(4 + args_to_push + fp_offset));
10900
      insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10901
      RTX_FRAME_RELATED_P (insn) = 1;
10902
 
10903
      if (IS_NESTED (func_type))
10904
        {
10905
          /* Recover the static chain register.  */
10906
          if (regs_ever_live [3] == 0
10907
              || saved_pretend_args)
10908
            insn = gen_rtx_REG (SImode, 3);
10909
          else /* if (current_function_pretend_args_size == 0) */
10910
            {
10911
              insn = plus_constant (hard_frame_pointer_rtx, 4);
10912
              insn = gen_frame_mem (SImode, insn);
10913
            }
10914
 
10915
          emit_set_insn (ip_rtx, insn);
10916
          /* Add a USE to stop propagate_one_insn() from barfing.  */
10917
          emit_insn (gen_prologue_use (ip_rtx));
10918
        }
10919
    }
10920
 
10921
  offsets = arm_get_frame_offsets ();
10922
  if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10923
    {
10924
      /* This add can produce multiple insns for a large constant, so we
10925
         need to get tricky.  */
10926
      rtx last = get_last_insn ();
10927
 
10928
      amount = GEN_INT (offsets->saved_args + saved_regs
10929
                        - offsets->outgoing_args);
10930
 
10931
      insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10932
                                    amount));
10933
      do
10934
        {
10935
          last = last ? NEXT_INSN (last) : get_insns ();
10936
          RTX_FRAME_RELATED_P (last) = 1;
10937
        }
10938
      while (last != insn);
10939
 
10940
      /* If the frame pointer is needed, emit a special barrier that
10941
         will prevent the scheduler from moving stores to the frame
10942
         before the stack adjustment.  */
10943
      if (frame_pointer_needed)
10944
        insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10945
                                         hard_frame_pointer_rtx));
10946
    }
10947
 
10948
 
10949
  if (flag_pic && arm_pic_register != INVALID_REGNUM)
10950
    arm_load_pic_register (0UL);
10951
 
10952
  /* If we are profiling, make sure no instructions are scheduled before
10953
     the call to mcount.  Similarly if the user has requested no
10954
     scheduling in the prolog.  Similarly if we want non-call exceptions
10955
     using the EABI unwinder, to prevent faulting instructions from being
10956
     swapped with a stack adjustment.  */
10957
  if (current_function_profile || !TARGET_SCHED_PROLOG
10958
      || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
10959
    emit_insn (gen_blockage ());
10960
 
10961
  /* If the link register is being kept alive, with the return address in it,
10962
     then make sure that it does not get reused by the ce2 pass.  */
10963
  if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10964
    {
10965
      emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10966
      cfun->machine->lr_save_eliminated = 1;
10967
    }
10968
}
10969
 
10970
/* If CODE is 'd', then the X is a condition operand and the instruction
10971
   should only be executed if the condition is true.
10972
   if CODE is 'D', then the X is a condition operand and the instruction
10973
   should only be executed if the condition is false: however, if the mode
10974
   of the comparison is CCFPEmode, then always execute the instruction -- we
10975
   do this because in these circumstances !GE does not necessarily imply LT;
10976
   in these cases the instruction pattern will take care to make sure that
10977
   an instruction containing %d will follow, thereby undoing the effects of
10978
   doing this instruction unconditionally.
10979
   If CODE is 'N' then X is a floating point operand that must be negated
10980
   before output.
10981
   If CODE is 'B' then output a bitwise inverted value of X (a const int).
10982
   If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10983
void
10984
arm_print_operand (FILE *stream, rtx x, int code)
10985
{
10986
  switch (code)
10987
    {
10988
    case '@':
10989
      fputs (ASM_COMMENT_START, stream);
10990
      return;
10991
 
10992
    case '_':
10993
      fputs (user_label_prefix, stream);
10994
      return;
10995
 
10996
    case '|':
10997
      fputs (REGISTER_PREFIX, stream);
10998
      return;
10999
 
11000
    case '?':
11001
      if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
11002
        {
11003
          if (TARGET_THUMB)
11004
            {
11005
              output_operand_lossage ("predicated Thumb instruction");
11006
              break;
11007
            }
11008
          if (current_insn_predicate != NULL)
11009
            {
11010
              output_operand_lossage
11011
                ("predicated instruction in conditional sequence");
11012
              break;
11013
            }
11014
 
11015
          fputs (arm_condition_codes[arm_current_cc], stream);
11016
        }
11017
      else if (current_insn_predicate)
11018
        {
11019
          enum arm_cond_code code;
11020
 
11021
          if (TARGET_THUMB)
11022
            {
11023
              output_operand_lossage ("predicated Thumb instruction");
11024
              break;
11025
            }
11026
 
11027
          code = get_arm_condition_code (current_insn_predicate);
11028
          fputs (arm_condition_codes[code], stream);
11029
        }
11030
      return;
11031
 
11032
    case 'N':
11033
      {
11034
        REAL_VALUE_TYPE r;
11035
        REAL_VALUE_FROM_CONST_DOUBLE (r, x);
11036
        r = REAL_VALUE_NEGATE (r);
11037
        fprintf (stream, "%s", fp_const_from_val (&r));
11038
      }
11039
      return;
11040
 
11041
    case 'B':
11042
      if (GET_CODE (x) == CONST_INT)
11043
        {
11044
          HOST_WIDE_INT val;
11045
          val = ARM_SIGN_EXTEND (~INTVAL (x));
11046
          fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
11047
        }
11048
      else
11049
        {
11050
          putc ('~', stream);
11051
          output_addr_const (stream, x);
11052
        }
11053
      return;
11054
 
11055
    case 'i':
11056
      fprintf (stream, "%s", arithmetic_instr (x, 1));
11057
      return;
11058
 
11059
    /* Truncate Cirrus shift counts.  */
11060
    case 's':
11061
      if (GET_CODE (x) == CONST_INT)
11062
        {
11063
          fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
11064
          return;
11065
        }
11066
      arm_print_operand (stream, x, 0);
11067
      return;
11068
 
11069
    case 'I':
11070
      fprintf (stream, "%s", arithmetic_instr (x, 0));
11071
      return;
11072
 
11073
    case 'S':
11074
      {
11075
        HOST_WIDE_INT val;
11076
        const char *shift;
11077
 
11078
        if (!shift_operator (x, SImode))
11079
          {
11080
            output_operand_lossage ("invalid shift operand");
11081
            break;
11082
          }
11083
 
11084
        shift = shift_op (x, &val);
11085
 
11086
        if (shift)
11087
          {
11088
            fprintf (stream, ", %s ", shift);
11089
            if (val == -1)
11090
              arm_print_operand (stream, XEXP (x, 1), 0);
11091
            else
11092
              fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
11093
          }
11094
      }
11095
      return;
11096
 
11097
      /* An explanation of the 'Q', 'R' and 'H' register operands:
11098
 
11099
         In a pair of registers containing a DI or DF value the 'Q'
11100
         operand returns the register number of the register containing
11101
         the least significant part of the value.  The 'R' operand returns
11102
         the register number of the register containing the most
11103
         significant part of the value.
11104
 
11105
         The 'H' operand returns the higher of the two register numbers.
11106
         On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
11107
         same as the 'Q' operand, since the most significant part of the
11108
         value is held in the lower number register.  The reverse is true
11109
         on systems where WORDS_BIG_ENDIAN is false.
11110
 
11111
         The purpose of these operands is to distinguish between cases
11112
         where the endian-ness of the values is important (for example
11113
         when they are added together), and cases where the endian-ness
11114
         is irrelevant, but the order of register operations is important.
11115
         For example when loading a value from memory into a register
11116
         pair, the endian-ness does not matter.  Provided that the value
11117
         from the lower memory address is put into the lower numbered
11118
         register, and the value from the higher address is put into the
11119
         higher numbered register, the load will work regardless of whether
11120
         the value being loaded is big-wordian or little-wordian.  The
11121
         order of the two register loads can matter however, if the address
11122
         of the memory location is actually held in one of the registers
11123
         being overwritten by the load.  */
11124
    case 'Q':
11125
      if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11126
        {
11127
          output_operand_lossage ("invalid operand for code '%c'", code);
11128
          return;
11129
        }
11130
 
11131
      asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
11132
      return;
11133
 
11134
    case 'R':
11135
      if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11136
        {
11137
          output_operand_lossage ("invalid operand for code '%c'", code);
11138
          return;
11139
        }
11140
 
11141
      asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
11142
      return;
11143
 
11144
    case 'H':
11145
      if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11146
        {
11147
          output_operand_lossage ("invalid operand for code '%c'", code);
11148
          return;
11149
        }
11150
 
11151
      asm_fprintf (stream, "%r", REGNO (x) + 1);
11152
      return;
11153
 
11154
    case 'm':
11155
      asm_fprintf (stream, "%r",
11156
                   GET_CODE (XEXP (x, 0)) == REG
11157
                   ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
11158
      return;
11159
 
11160
    case 'M':
11161
      asm_fprintf (stream, "{%r-%r}",
11162
                   REGNO (x),
11163
                   REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
11164
      return;
11165
 
11166
    case 'd':
11167
      /* CONST_TRUE_RTX means always -- that's the default.  */
11168
      if (x == const_true_rtx)
11169
        return;
11170
 
11171
      if (!COMPARISON_P (x))
11172
        {
11173
          output_operand_lossage ("invalid operand for code '%c'", code);
11174
          return;
11175
        }
11176
 
11177
      fputs (arm_condition_codes[get_arm_condition_code (x)],
11178
             stream);
11179
      return;
11180
 
11181
    case 'D':
11182
      /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
11183
         want to do that.  */
11184
      if (x == const_true_rtx)
11185
        {
11186
          output_operand_lossage ("instruction never exectued");
11187
          return;
11188
        }
11189
      if (!COMPARISON_P (x))
11190
        {
11191
          output_operand_lossage ("invalid operand for code '%c'", code);
11192
          return;
11193
        }
11194
 
11195
      fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
11196
                                 (get_arm_condition_code (x))],
11197
             stream);
11198
      return;
11199
 
11200
    /* Cirrus registers can be accessed in a variety of ways:
11201
         single floating point (f)
11202
         double floating point (d)
11203
         32bit integer         (fx)
11204
         64bit integer         (dx).  */
11205
    case 'W':                   /* Cirrus register in F mode.  */
11206
    case 'X':                   /* Cirrus register in D mode.  */
11207
    case 'Y':                   /* Cirrus register in FX mode.  */
11208
    case 'Z':                   /* Cirrus register in DX mode.  */
11209
      gcc_assert (GET_CODE (x) == REG
11210
                  && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
11211
 
11212
      fprintf (stream, "mv%s%s",
11213
               code == 'W' ? "f"
11214
               : code == 'X' ? "d"
11215
               : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
11216
 
11217
      return;
11218
 
11219
    /* Print cirrus register in the mode specified by the register's mode.  */
11220
    case 'V':
11221
      {
11222
        int mode = GET_MODE (x);
11223
 
11224
        if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
11225
          {
11226
            output_operand_lossage ("invalid operand for code '%c'", code);
11227
            return;
11228
          }
11229
 
11230
        fprintf (stream, "mv%s%s",
11231
                 mode == DFmode ? "d"
11232
                 : mode == SImode ? "fx"
11233
                 : mode == DImode ? "dx"
11234
                 : "f", reg_names[REGNO (x)] + 2);
11235
 
11236
        return;
11237
      }
11238
 
11239
    case 'U':
11240
      if (GET_CODE (x) != REG
11241
          || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
11242
          || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
11243
        /* Bad value for wCG register number.  */
11244
        {
11245
          output_operand_lossage ("invalid operand for code '%c'", code);
11246
          return;
11247
        }
11248
 
11249
      else
11250
        fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
11251
      return;
11252
 
11253
      /* Print an iWMMXt control register name.  */
11254
    case 'w':
11255
      if (GET_CODE (x) != CONST_INT
11256
          || INTVAL (x) < 0
11257
          || INTVAL (x) >= 16)
11258
        /* Bad value for wC register number.  */
11259
        {
11260
          output_operand_lossage ("invalid operand for code '%c'", code);
11261
          return;
11262
        }
11263
 
11264
      else
11265
        {
11266
          static const char * wc_reg_names [16] =
11267
            {
11268
              "wCID",  "wCon",  "wCSSF", "wCASF",
11269
              "wC4",   "wC5",   "wC6",   "wC7",
11270
              "wCGR0", "wCGR1", "wCGR2", "wCGR3",
11271
              "wC12",  "wC13",  "wC14",  "wC15"
11272
            };
11273
 
11274
          fprintf (stream, wc_reg_names [INTVAL (x)]);
11275
        }
11276
      return;
11277
 
11278
      /* Print a VFP double precision register name.  */
11279
    case 'P':
11280
      {
11281
        int mode = GET_MODE (x);
11282
        int num;
11283
 
11284
        if (mode != DImode && mode != DFmode)
11285
          {
11286
            output_operand_lossage ("invalid operand for code '%c'", code);
11287
            return;
11288
          }
11289
 
11290
        if (GET_CODE (x) != REG
11291
            || !IS_VFP_REGNUM (REGNO (x)))
11292
          {
11293
            output_operand_lossage ("invalid operand for code '%c'", code);
11294
            return;
11295
          }
11296
 
11297
        num = REGNO(x) - FIRST_VFP_REGNUM;
11298
        if (num & 1)
11299
          {
11300
            output_operand_lossage ("invalid operand for code '%c'", code);
11301
            return;
11302
          }
11303
 
11304
        fprintf (stream, "d%d", num >> 1);
11305
      }
11306
      return;
11307
 
11308
    default:
11309
      if (x == 0)
11310
        {
11311
          output_operand_lossage ("missing operand");
11312
          return;
11313
        }
11314
 
11315
      switch (GET_CODE (x))
11316
        {
11317
        case REG:
11318
          asm_fprintf (stream, "%r", REGNO (x));
11319
          break;
11320
 
11321
        case MEM:
11322
          output_memory_reference_mode = GET_MODE (x);
11323
          output_address (XEXP (x, 0));
11324
          break;
11325
 
11326
        case CONST_DOUBLE:
11327
          fprintf (stream, "#%s", fp_immediate_constant (x));
11328
          break;
11329
 
11330
        default:
11331
          gcc_assert (GET_CODE (x) != NEG);
11332
          fputc ('#', stream);
11333
          output_addr_const (stream, x);
11334
          break;
11335
        }
11336
    }
11337
}
11338
 
11339
#ifndef AOF_ASSEMBLER
11340
/* Target hook for assembling integer objects.  The ARM version needs to
11341
   handle word-sized values specially.  */
11342
static bool
11343
arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
11344
{
11345
  if (size == UNITS_PER_WORD && aligned_p)
11346
    {
11347
      fputs ("\t.word\t", asm_out_file);
11348
      output_addr_const (asm_out_file, x);
11349
 
11350
      /* Mark symbols as position independent.  We only do this in the
11351
         .text segment, not in the .data segment.  */
11352
      if (NEED_GOT_RELOC && flag_pic && making_const_table &&
11353
          (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
11354
        {
11355
          if (GET_CODE (x) == SYMBOL_REF
11356
              && (CONSTANT_POOL_ADDRESS_P (x)
11357
                  || SYMBOL_REF_LOCAL_P (x)))
11358
            fputs ("(GOTOFF)", asm_out_file);
11359
          else if (GET_CODE (x) == LABEL_REF)
11360
            fputs ("(GOTOFF)", asm_out_file);
11361
          else
11362
            fputs ("(GOT)", asm_out_file);
11363
        }
11364
      fputc ('\n', asm_out_file);
11365
      return true;
11366
    }
11367
 
11368
  if (arm_vector_mode_supported_p (GET_MODE (x)))
11369
    {
11370
      int i, units;
11371
 
11372
      gcc_assert (GET_CODE (x) == CONST_VECTOR);
11373
 
11374
      units = CONST_VECTOR_NUNITS (x);
11375
 
11376
      switch (GET_MODE (x))
11377
        {
11378
        case V2SImode: size = 4; break;
11379
        case V4HImode: size = 2; break;
11380
        case V8QImode: size = 1; break;
11381
        default:
11382
          gcc_unreachable ();
11383
        }
11384
 
11385
      for (i = 0; i < units; i++)
11386
        {
11387
          rtx elt;
11388
 
11389
          elt = CONST_VECTOR_ELT (x, i);
11390
          assemble_integer
11391
            (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
11392
        }
11393
 
11394
      return true;
11395
    }
11396
 
11397
  return default_assemble_integer (x, size, aligned_p);
11398
}
11399
 
11400
 
11401
/* Add a function to the list of static constructors.  */
11402
 
11403
static void
11404
arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
11405
{
11406
  if (!TARGET_AAPCS_BASED)
11407
    {
11408
      default_named_section_asm_out_constructor (symbol, priority);
11409
      return;
11410
    }
11411
 
11412
  /* Put these in the .init_array section, using a special relocation.  */
11413
  switch_to_section (ctors_section);
11414
  assemble_align (POINTER_SIZE);
11415
  fputs ("\t.word\t", asm_out_file);
11416
  output_addr_const (asm_out_file, symbol);
11417
  fputs ("(target1)\n", asm_out_file);
11418
}
11419
#endif
11420
 
11421
/* A finite state machine takes care of noticing whether or not instructions
11422
   can be conditionally executed, and thus decrease execution time and code
11423
   size by deleting branch instructions.  The fsm is controlled by
11424
   final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
11425
 
11426
/* The state of the fsm controlling condition codes are:
11427
   0: normal, do nothing special
11428
   1: make ASM_OUTPUT_OPCODE not output this instruction
11429
   2: make ASM_OUTPUT_OPCODE not output this instruction
11430
   3: make instructions conditional
11431
   4: make instructions conditional
11432
 
11433
   State transitions (state->state by whom under condition):
11434
 
11435
 
11436
   1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
11437
   2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
11438
   3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
11439
          (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
11440
   4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
11441
          (the target insn is arm_target_insn).
11442
 
11443
   If the jump clobbers the conditions then we use states 2 and 4.
11444
 
11445
   A similar thing can be done with conditional return insns.
11446
 
11447
   XXX In case the `target' is an unconditional branch, this conditionalising
11448
   of the instructions always reduces code size, but not always execution
11449
   time.  But then, I want to reduce the code size to somewhere near what
11450
   /bin/cc produces.  */
11451
 
11452
/* Returns the index of the ARM condition code string in
11453
   `arm_condition_codes'.  COMPARISON should be an rtx like
11454
   `(eq (...) (...))'.  */
11455
static enum arm_cond_code
11456
get_arm_condition_code (rtx comparison)
11457
{
11458
  enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
11459
  int code;
11460
  enum rtx_code comp_code = GET_CODE (comparison);
11461
 
11462
  if (GET_MODE_CLASS (mode) != MODE_CC)
11463
    mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
11464
                           XEXP (comparison, 1));
11465
 
11466
  switch (mode)
11467
    {
11468
    case CC_DNEmode: code = ARM_NE; goto dominance;
11469
    case CC_DEQmode: code = ARM_EQ; goto dominance;
11470
    case CC_DGEmode: code = ARM_GE; goto dominance;
11471
    case CC_DGTmode: code = ARM_GT; goto dominance;
11472
    case CC_DLEmode: code = ARM_LE; goto dominance;
11473
    case CC_DLTmode: code = ARM_LT; goto dominance;
11474
    case CC_DGEUmode: code = ARM_CS; goto dominance;
11475
    case CC_DGTUmode: code = ARM_HI; goto dominance;
11476
    case CC_DLEUmode: code = ARM_LS; goto dominance;
11477
    case CC_DLTUmode: code = ARM_CC;
11478
 
11479
    dominance:
11480
      gcc_assert (comp_code == EQ || comp_code == NE);
11481
 
11482
      if (comp_code == EQ)
11483
        return ARM_INVERSE_CONDITION_CODE (code);
11484
      return code;
11485
 
11486
    case CC_NOOVmode:
11487
      switch (comp_code)
11488
        {
11489
        case NE: return ARM_NE;
11490
        case EQ: return ARM_EQ;
11491
        case GE: return ARM_PL;
11492
        case LT: return ARM_MI;
11493
        default: gcc_unreachable ();
11494
        }
11495
 
11496
    case CC_Zmode:
11497
      switch (comp_code)
11498
        {
11499
        case NE: return ARM_NE;
11500
        case EQ: return ARM_EQ;
11501
        default: gcc_unreachable ();
11502
        }
11503
 
11504
    case CC_Nmode:
11505
      switch (comp_code)
11506
        {
11507
        case NE: return ARM_MI;
11508
        case EQ: return ARM_PL;
11509
        default: gcc_unreachable ();
11510
        }
11511
 
11512
    case CCFPEmode:
11513
    case CCFPmode:
11514
      /* These encodings assume that AC=1 in the FPA system control
11515
         byte.  This allows us to handle all cases except UNEQ and
11516
         LTGT.  */
11517
      switch (comp_code)
11518
        {
11519
        case GE: return ARM_GE;
11520
        case GT: return ARM_GT;
11521
        case LE: return ARM_LS;
11522
        case LT: return ARM_MI;
11523
        case NE: return ARM_NE;
11524
        case EQ: return ARM_EQ;
11525
        case ORDERED: return ARM_VC;
11526
        case UNORDERED: return ARM_VS;
11527
        case UNLT: return ARM_LT;
11528
        case UNLE: return ARM_LE;
11529
        case UNGT: return ARM_HI;
11530
        case UNGE: return ARM_PL;
11531
          /* UNEQ and LTGT do not have a representation.  */
11532
        case UNEQ: /* Fall through.  */
11533
        case LTGT: /* Fall through.  */
11534
        default: gcc_unreachable ();
11535
        }
11536
 
11537
    case CC_SWPmode:
11538
      switch (comp_code)
11539
        {
11540
        case NE: return ARM_NE;
11541
        case EQ: return ARM_EQ;
11542
        case GE: return ARM_LE;
11543
        case GT: return ARM_LT;
11544
        case LE: return ARM_GE;
11545
        case LT: return ARM_GT;
11546
        case GEU: return ARM_LS;
11547
        case GTU: return ARM_CC;
11548
        case LEU: return ARM_CS;
11549
        case LTU: return ARM_HI;
11550
        default: gcc_unreachable ();
11551
        }
11552
 
11553
    case CC_Cmode:
11554
      switch (comp_code)
11555
      {
11556
      case LTU: return ARM_CS;
11557
      case GEU: return ARM_CC;
11558
      default: gcc_unreachable ();
11559
      }
11560
 
11561
    case CCmode:
11562
      switch (comp_code)
11563
        {
11564
        case NE: return ARM_NE;
11565
        case EQ: return ARM_EQ;
11566
        case GE: return ARM_GE;
11567
        case GT: return ARM_GT;
11568
        case LE: return ARM_LE;
11569
        case LT: return ARM_LT;
11570
        case GEU: return ARM_CS;
11571
        case GTU: return ARM_HI;
11572
        case LEU: return ARM_LS;
11573
        case LTU: return ARM_CC;
11574
        default: gcc_unreachable ();
11575
        }
11576
 
11577
    default: gcc_unreachable ();
11578
    }
11579
}
11580
 
11581
void
11582
arm_final_prescan_insn (rtx insn)
11583
{
11584
  /* BODY will hold the body of INSN.  */
11585
  rtx body = PATTERN (insn);
11586
 
11587
  /* This will be 1 if trying to repeat the trick, and things need to be
11588
     reversed if it appears to fail.  */
11589
  int reverse = 0;
11590
 
11591
  /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11592
     taken are clobbered, even if the rtl suggests otherwise.  It also
11593
     means that we have to grub around within the jump expression to find
11594
     out what the conditions are when the jump isn't taken.  */
11595
  int jump_clobbers = 0;
11596
 
11597
  /* If we start with a return insn, we only succeed if we find another one.  */
11598
  int seeking_return = 0;
11599
 
11600
  /* START_INSN will hold the insn from where we start looking.  This is the
11601
     first insn after the following code_label if REVERSE is true.  */
11602
  rtx start_insn = insn;
11603
 
11604
  /* If in state 4, check if the target branch is reached, in order to
11605
     change back to state 0.  */
11606
  if (arm_ccfsm_state == 4)
11607
    {
11608
      if (insn == arm_target_insn)
11609
        {
11610
          arm_target_insn = NULL;
11611
          arm_ccfsm_state = 0;
11612
        }
11613
      return;
11614
    }
11615
 
11616
  /* If in state 3, it is possible to repeat the trick, if this insn is an
11617
     unconditional branch to a label, and immediately following this branch
11618
     is the previous target label which is only used once, and the label this
11619
     branch jumps to is not too far off.  */
11620
  if (arm_ccfsm_state == 3)
11621
    {
11622
      if (simplejump_p (insn))
11623
        {
11624
          start_insn = next_nonnote_insn (start_insn);
11625
          if (GET_CODE (start_insn) == BARRIER)
11626
            {
11627
              /* XXX Isn't this always a barrier?  */
11628
              start_insn = next_nonnote_insn (start_insn);
11629
            }
11630
          if (GET_CODE (start_insn) == CODE_LABEL
11631
              && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11632
              && LABEL_NUSES (start_insn) == 1)
11633
            reverse = TRUE;
11634
          else
11635
            return;
11636
        }
11637
      else if (GET_CODE (body) == RETURN)
11638
        {
11639
          start_insn = next_nonnote_insn (start_insn);
11640
          if (GET_CODE (start_insn) == BARRIER)
11641
            start_insn = next_nonnote_insn (start_insn);
11642
          if (GET_CODE (start_insn) == CODE_LABEL
11643
              && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11644
              && LABEL_NUSES (start_insn) == 1)
11645
            {
11646
              reverse = TRUE;
11647
              seeking_return = 1;
11648
            }
11649
          else
11650
            return;
11651
        }
11652
      else
11653
        return;
11654
    }
11655
 
11656
  gcc_assert (!arm_ccfsm_state || reverse);
11657
  if (GET_CODE (insn) != JUMP_INSN)
11658
    return;
11659
 
11660
  /* This jump might be paralleled with a clobber of the condition codes
11661
     the jump should always come first */
11662
  if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11663
    body = XVECEXP (body, 0, 0);
11664
 
11665
  if (reverse
11666
      || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11667
          && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11668
    {
11669
      int insns_skipped;
11670
      int fail = FALSE, succeed = FALSE;
11671
      /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11672
      int then_not_else = TRUE;
11673
      rtx this_insn = start_insn, label = 0;
11674
 
11675
      /* If the jump cannot be done with one instruction, we cannot
11676
         conditionally execute the instruction in the inverse case.  */
11677
      if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11678
        {
11679
          jump_clobbers = 1;
11680
          return;
11681
        }
11682
 
11683
      /* Register the insn jumped to.  */
11684
      if (reverse)
11685
        {
11686
          if (!seeking_return)
11687
            label = XEXP (SET_SRC (body), 0);
11688
        }
11689
      else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11690
        label = XEXP (XEXP (SET_SRC (body), 1), 0);
11691
      else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11692
        {
11693
          label = XEXP (XEXP (SET_SRC (body), 2), 0);
11694
          then_not_else = FALSE;
11695
        }
11696
      else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11697
        seeking_return = 1;
11698
      else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11699
        {
11700
          seeking_return = 1;
11701
          then_not_else = FALSE;
11702
        }
11703
      else
11704
        gcc_unreachable ();
11705
 
11706
      /* See how many insns this branch skips, and what kind of insns.  If all
11707
         insns are okay, and the label or unconditional branch to the same
11708
         label is not too far away, succeed.  */
11709
      for (insns_skipped = 0;
11710
           !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11711
        {
11712
          rtx scanbody;
11713
 
11714
          this_insn = next_nonnote_insn (this_insn);
11715
          if (!this_insn)
11716
            break;
11717
 
11718
          switch (GET_CODE (this_insn))
11719
            {
11720
            case CODE_LABEL:
11721
              /* Succeed if it is the target label, otherwise fail since
11722
                 control falls in from somewhere else.  */
11723
              if (this_insn == label)
11724
                {
11725
                  if (jump_clobbers)
11726
                    {
11727
                      arm_ccfsm_state = 2;
11728
                      this_insn = next_nonnote_insn (this_insn);
11729
                    }
11730
                  else
11731
                    arm_ccfsm_state = 1;
11732
                  succeed = TRUE;
11733
                }
11734
              else
11735
                fail = TRUE;
11736
              break;
11737
 
11738
            case BARRIER:
11739
              /* Succeed if the following insn is the target label.
11740
                 Otherwise fail.
11741
                 If return insns are used then the last insn in a function
11742
                 will be a barrier.  */
11743
              this_insn = next_nonnote_insn (this_insn);
11744
              if (this_insn && this_insn == label)
11745
                {
11746
                  if (jump_clobbers)
11747
                    {
11748
                      arm_ccfsm_state = 2;
11749
                      this_insn = next_nonnote_insn (this_insn);
11750
                    }
11751
                  else
11752
                    arm_ccfsm_state = 1;
11753
                  succeed = TRUE;
11754
                }
11755
              else
11756
                fail = TRUE;
11757
              break;
11758
 
11759
            case CALL_INSN:
11760
              /* The AAPCS says that conditional calls should not be
11761
                 used since they make interworking inefficient (the
11762
                 linker can't transform BL<cond> into BLX).  That's
11763
                 only a problem if the machine has BLX.  */
11764
              if (arm_arch5)
11765
                {
11766
                  fail = TRUE;
11767
                  break;
11768
                }
11769
 
11770
              /* Succeed if the following insn is the target label, or
11771
                 if the following two insns are a barrier and the
11772
                 target label.  */
11773
              this_insn = next_nonnote_insn (this_insn);
11774
              if (this_insn && GET_CODE (this_insn) == BARRIER)
11775
                this_insn = next_nonnote_insn (this_insn);
11776
 
11777
              if (this_insn && this_insn == label
11778
                  && insns_skipped < max_insns_skipped)
11779
                {
11780
                  if (jump_clobbers)
11781
                    {
11782
                      arm_ccfsm_state = 2;
11783
                      this_insn = next_nonnote_insn (this_insn);
11784
                    }
11785
                  else
11786
                    arm_ccfsm_state = 1;
11787
                  succeed = TRUE;
11788
                }
11789
              else
11790
                fail = TRUE;
11791
              break;
11792
 
11793
            case JUMP_INSN:
11794
              /* If this is an unconditional branch to the same label, succeed.
11795
                 If it is to another label, do nothing.  If it is conditional,
11796
                 fail.  */
11797
              /* XXX Probably, the tests for SET and the PC are
11798
                 unnecessary.  */
11799
 
11800
              scanbody = PATTERN (this_insn);
11801
              if (GET_CODE (scanbody) == SET
11802
                  && GET_CODE (SET_DEST (scanbody)) == PC)
11803
                {
11804
                  if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11805
                      && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11806
                    {
11807
                      arm_ccfsm_state = 2;
11808
                      succeed = TRUE;
11809
                    }
11810
                  else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11811
                    fail = TRUE;
11812
                }
11813
              /* Fail if a conditional return is undesirable (e.g. on a
11814
                 StrongARM), but still allow this if optimizing for size.  */
11815
              else if (GET_CODE (scanbody) == RETURN
11816
                       && !use_return_insn (TRUE, NULL)
11817
                       && !optimize_size)
11818
                fail = TRUE;
11819
              else if (GET_CODE (scanbody) == RETURN
11820
                       && seeking_return)
11821
                {
11822
                  arm_ccfsm_state = 2;
11823
                  succeed = TRUE;
11824
                }
11825
              else if (GET_CODE (scanbody) == PARALLEL)
11826
                {
11827
                  switch (get_attr_conds (this_insn))
11828
                    {
11829
                    case CONDS_NOCOND:
11830
                      break;
11831
                    default:
11832
                      fail = TRUE;
11833
                      break;
11834
                    }
11835
                }
11836
              else
11837
                fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11838
 
11839
              break;
11840
 
11841
            case INSN:
11842
              /* Instructions using or affecting the condition codes make it
11843
                 fail.  */
11844
              scanbody = PATTERN (this_insn);
11845
              if (!(GET_CODE (scanbody) == SET
11846
                    || GET_CODE (scanbody) == PARALLEL)
11847
                  || get_attr_conds (this_insn) != CONDS_NOCOND)
11848
                fail = TRUE;
11849
 
11850
              /* A conditional cirrus instruction must be followed by
11851
                 a non Cirrus instruction.  However, since we
11852
                 conditionalize instructions in this function and by
11853
                 the time we get here we can't add instructions
11854
                 (nops), because shorten_branches() has already been
11855
                 called, we will disable conditionalizing Cirrus
11856
                 instructions to be safe.  */
11857
              if (GET_CODE (scanbody) != USE
11858
                  && GET_CODE (scanbody) != CLOBBER
11859
                  && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11860
                fail = TRUE;
11861
              break;
11862
 
11863
            default:
11864
              break;
11865
            }
11866
        }
11867
      if (succeed)
11868
        {
11869
          if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11870
            arm_target_label = CODE_LABEL_NUMBER (label);
11871
          else
11872
            {
11873
              gcc_assert (seeking_return || arm_ccfsm_state == 2);
11874
 
11875
              while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11876
                {
11877
                  this_insn = next_nonnote_insn (this_insn);
11878
                  gcc_assert (!this_insn
11879
                              || (GET_CODE (this_insn) != BARRIER
11880
                                  && GET_CODE (this_insn) != CODE_LABEL));
11881
                }
11882
              if (!this_insn)
11883
                {
11884
                  /* Oh, dear! we ran off the end.. give up.  */
11885
                  recog (PATTERN (insn), insn, NULL);
11886
                  arm_ccfsm_state = 0;
11887
                  arm_target_insn = NULL;
11888
                  return;
11889
                }
11890
              arm_target_insn = this_insn;
11891
            }
11892
          if (jump_clobbers)
11893
            {
11894
              gcc_assert (!reverse);
11895
              arm_current_cc =
11896
                  get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11897
                                                            0), 0), 1));
11898
              if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11899
                arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11900
              if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11901
                arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11902
            }
11903
          else
11904
            {
11905
              /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11906
                 what it was.  */
11907
              if (!reverse)
11908
                arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11909
                                                               0));
11910
            }
11911
 
11912
          if (reverse || then_not_else)
11913
            arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11914
        }
11915
 
11916
      /* Restore recog_data (getting the attributes of other insns can
11917
         destroy this array, but final.c assumes that it remains intact
11918
         across this call; since the insn has been recognized already we
11919
         call recog direct).  */
11920
      recog (PATTERN (insn), insn, NULL);
11921
    }
11922
}
11923
 
11924
/* Returns true if REGNO is a valid register
11925
   for holding a quantity of type MODE.  */
11926
int
11927
arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11928
{
11929
  if (GET_MODE_CLASS (mode) == MODE_CC)
11930
    return (regno == CC_REGNUM
11931
            || (TARGET_HARD_FLOAT && TARGET_VFP
11932
                && regno == VFPCC_REGNUM));
11933
 
11934
  if (TARGET_THUMB)
11935
    /* For the Thumb we only allow values bigger than SImode in
11936
       registers 0 - 6, so that there is always a second low
11937
       register available to hold the upper part of the value.
11938
       We probably we ought to ensure that the register is the
11939
       start of an even numbered register pair.  */
11940
    return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11941
 
11942
  if (TARGET_HARD_FLOAT && TARGET_MAVERICK
11943
      && IS_CIRRUS_REGNUM (regno))
11944
    /* We have outlawed SI values in Cirrus registers because they
11945
       reside in the lower 32 bits, but SF values reside in the
11946
       upper 32 bits.  This causes gcc all sorts of grief.  We can't
11947
       even split the registers into pairs because Cirrus SI values
11948
       get sign extended to 64bits-- aldyh.  */
11949
    return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11950
 
11951
  if (TARGET_HARD_FLOAT && TARGET_VFP
11952
      && IS_VFP_REGNUM (regno))
11953
    {
11954
      if (mode == SFmode || mode == SImode)
11955
        return TRUE;
11956
 
11957
      /* DFmode values are only valid in even register pairs.  */
11958
      if (mode == DFmode)
11959
        return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11960
      return FALSE;
11961
    }
11962
 
11963
  if (TARGET_REALLY_IWMMXT)
11964
    {
11965
      if (IS_IWMMXT_GR_REGNUM (regno))
11966
        return mode == SImode;
11967
 
11968
      if (IS_IWMMXT_REGNUM (regno))
11969
        return VALID_IWMMXT_REG_MODE (mode);
11970
    }
11971
 
11972
  /* We allow any value to be stored in the general registers.
11973
     Restrict doubleword quantities to even register pairs so that we can
11974
     use ldrd.  */
11975
  if (regno <= LAST_ARM_REGNUM)
11976
    return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11977
 
11978
  if (regno == FRAME_POINTER_REGNUM
11979
      || regno == ARG_POINTER_REGNUM)
11980
    /* We only allow integers in the fake hard registers.  */
11981
    return GET_MODE_CLASS (mode) == MODE_INT;
11982
 
11983
  /* The only registers left are the FPA registers
11984
     which we only allow to hold FP values.  */
11985
  return (TARGET_HARD_FLOAT && TARGET_FPA
11986
          && GET_MODE_CLASS (mode) == MODE_FLOAT
11987
          && regno >= FIRST_FPA_REGNUM
11988
          && regno <= LAST_FPA_REGNUM);
11989
}
11990
 
11991
int
11992
arm_regno_class (int regno)
11993
{
11994
  if (TARGET_THUMB)
11995
    {
11996
      if (regno == STACK_POINTER_REGNUM)
11997
        return STACK_REG;
11998
      if (regno == CC_REGNUM)
11999
        return CC_REG;
12000
      if (regno < 8)
12001
        return LO_REGS;
12002
      return HI_REGS;
12003
    }
12004
 
12005
  if (   regno <= LAST_ARM_REGNUM
12006
      || regno == FRAME_POINTER_REGNUM
12007
      || regno == ARG_POINTER_REGNUM)
12008
    return GENERAL_REGS;
12009
 
12010
  if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
12011
    return NO_REGS;
12012
 
12013
  if (IS_CIRRUS_REGNUM (regno))
12014
    return CIRRUS_REGS;
12015
 
12016
  if (IS_VFP_REGNUM (regno))
12017
    return VFP_REGS;
12018
 
12019
  if (IS_IWMMXT_REGNUM (regno))
12020
    return IWMMXT_REGS;
12021
 
12022
  if (IS_IWMMXT_GR_REGNUM (regno))
12023
    return IWMMXT_GR_REGS;
12024
 
12025
  return FPA_REGS;
12026
}
12027
 
12028
/* Handle a special case when computing the offset
12029
   of an argument from the frame pointer.  */
12030
int
12031
arm_debugger_arg_offset (int value, rtx addr)
12032
{
12033
  rtx insn;
12034
 
12035
  /* We are only interested if dbxout_parms() failed to compute the offset.  */
12036
  if (value != 0)
12037
    return 0;
12038
 
12039
  /* We can only cope with the case where the address is held in a register.  */
12040
  if (GET_CODE (addr) != REG)
12041
    return 0;
12042
 
12043
  /* If we are using the frame pointer to point at the argument, then
12044
     an offset of 0 is correct.  */
12045
  if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
12046
    return 0;
12047
 
12048
  /* If we are using the stack pointer to point at the
12049
     argument, then an offset of 0 is correct.  */
12050
  if ((TARGET_THUMB || !frame_pointer_needed)
12051
      && REGNO (addr) == SP_REGNUM)
12052
    return 0;
12053
 
12054
  /* Oh dear.  The argument is pointed to by a register rather
12055
     than being held in a register, or being stored at a known
12056
     offset from the frame pointer.  Since GDB only understands
12057
     those two kinds of argument we must translate the address
12058
     held in the register into an offset from the frame pointer.
12059
     We do this by searching through the insns for the function
12060
     looking to see where this register gets its value.  If the
12061
     register is initialized from the frame pointer plus an offset
12062
     then we are in luck and we can continue, otherwise we give up.
12063
 
12064
     This code is exercised by producing debugging information
12065
     for a function with arguments like this:
12066
 
12067
           double func (double a, double b, int c, double d) {return d;}
12068
 
12069
     Without this code the stab for parameter 'd' will be set to
12070
     an offset of 0 from the frame pointer, rather than 8.  */
12071
 
12072
  /* The if() statement says:
12073
 
12074
     If the insn is a normal instruction
12075
     and if the insn is setting the value in a register
12076
     and if the register being set is the register holding the address of the argument
12077
     and if the address is computing by an addition
12078
     that involves adding to a register
12079
     which is the frame pointer
12080
     a constant integer
12081
 
12082
     then...  */
12083
 
12084
  for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12085
    {
12086
      if (   GET_CODE (insn) == INSN
12087
          && GET_CODE (PATTERN (insn)) == SET
12088
          && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
12089
          && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
12090
          && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
12091
          && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
12092
          && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
12093
             )
12094
        {
12095
          value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
12096
 
12097
          break;
12098
        }
12099
    }
12100
 
12101
  if (value == 0)
12102
    {
12103
      debug_rtx (addr);
12104
      warning (0, "unable to compute real location of stacked parameter");
12105
      value = 8; /* XXX magic hack */
12106
    }
12107
 
12108
  return value;
12109
}
12110
 
12111
#define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
12112
  do                                                                    \
12113
    {                                                                   \
12114
      if ((MASK) & insn_flags)                                          \
12115
        lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
12116
                                     BUILT_IN_MD, NULL, NULL_TREE);     \
12117
    }                                                                   \
12118
  while (0)
12119
 
12120
struct builtin_description
12121
{
12122
  const unsigned int       mask;
12123
  const enum insn_code     icode;
12124
  const char * const       name;
12125
  const enum arm_builtins  code;
12126
  const enum rtx_code      comparison;
12127
  const unsigned int       flag;
12128
};
12129
 
12130
static const struct builtin_description bdesc_2arg[] =
12131
{
12132
#define IWMMXT_BUILTIN(code, string, builtin) \
12133
  { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
12134
    ARM_BUILTIN_##builtin, 0, 0 },
12135
 
12136
  IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
12137
  IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
12138
  IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
12139
  IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
12140
  IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
12141
  IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
12142
  IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
12143
  IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
12144
  IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
12145
  IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
12146
  IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
12147
  IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
12148
  IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
12149
  IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
12150
  IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
12151
  IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
12152
  IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
12153
  IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
12154
  IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
12155
  IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
12156
  IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
12157
  IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
12158
  IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
12159
  IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
12160
  IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
12161
  IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
12162
  IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
12163
  IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
12164
  IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
12165
  IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
12166
  IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
12167
  IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
12168
  IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
12169
  IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
12170
  IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
12171
  IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
12172
  IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
12173
  IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
12174
  IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
12175
  IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
12176
  IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
12177
  IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
12178
  IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
12179
  IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
12180
  IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
12181
  IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
12182
  IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
12183
  IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
12184
  IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
12185
  IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
12186
  IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
12187
  IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
12188
  IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
12189
  IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
12190
  IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
12191
  IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
12192
  IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
12193
  IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
12194
 
12195
#define IWMMXT_BUILTIN2(code, builtin) \
12196
  { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
12197
 
12198
  IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
12199
  IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
12200
  IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
12201
  IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
12202
  IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
12203
  IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
12204
  IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
12205
  IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
12206
  IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
12207
  IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
12208
  IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
12209
  IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
12210
  IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
12211
  IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
12212
  IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
12213
  IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
12214
  IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
12215
  IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
12216
  IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
12217
  IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
12218
  IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
12219
  IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
12220
  IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
12221
  IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
12222
  IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
12223
  IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
12224
  IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
12225
  IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
12226
  IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
12227
  IWMMXT_BUILTIN2 (rordi3,          WRORDI)
12228
  IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
12229
  IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
12230
};
12231
 
12232
static const struct builtin_description bdesc_1arg[] =
12233
{
12234
  IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
12235
  IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
12236
  IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
12237
  IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
12238
  IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
12239
  IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
12240
  IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
12241
  IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
12242
  IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
12243
  IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
12244
  IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
12245
  IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
12246
  IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
12247
  IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
12248
  IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
12249
  IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
12250
  IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
12251
  IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
12252
};
12253
 
12254
/* Set up all the iWMMXt builtins.  This is
12255
   not called if TARGET_IWMMXT is zero.  */
12256
 
12257
static void
12258
arm_init_iwmmxt_builtins (void)
12259
{
12260
  const struct builtin_description * d;
12261
  size_t i;
12262
  tree endlink = void_list_node;
12263
 
12264
  tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12265
  tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
12266
  tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
12267
 
12268
  tree int_ftype_int
12269
    = build_function_type (integer_type_node,
12270
                           tree_cons (NULL_TREE, integer_type_node, endlink));
12271
  tree v8qi_ftype_v8qi_v8qi_int
12272
    = build_function_type (V8QI_type_node,
12273
                           tree_cons (NULL_TREE, V8QI_type_node,
12274
                                      tree_cons (NULL_TREE, V8QI_type_node,
12275
                                                 tree_cons (NULL_TREE,
12276
                                                            integer_type_node,
12277
                                                            endlink))));
12278
  tree v4hi_ftype_v4hi_int
12279
    = build_function_type (V4HI_type_node,
12280
                           tree_cons (NULL_TREE, V4HI_type_node,
12281
                                      tree_cons (NULL_TREE, integer_type_node,
12282
                                                 endlink)));
12283
  tree v2si_ftype_v2si_int
12284
    = build_function_type (V2SI_type_node,
12285
                           tree_cons (NULL_TREE, V2SI_type_node,
12286
                                      tree_cons (NULL_TREE, integer_type_node,
12287
                                                 endlink)));
12288
  tree v2si_ftype_di_di
12289
    = build_function_type (V2SI_type_node,
12290
                           tree_cons (NULL_TREE, long_long_integer_type_node,
12291
                                      tree_cons (NULL_TREE, long_long_integer_type_node,
12292
                                                 endlink)));
12293
  tree di_ftype_di_int
12294
    = build_function_type (long_long_integer_type_node,
12295
                           tree_cons (NULL_TREE, long_long_integer_type_node,
12296
                                      tree_cons (NULL_TREE, integer_type_node,
12297
                                                 endlink)));
12298
  tree di_ftype_di_int_int
12299
    = build_function_type (long_long_integer_type_node,
12300
                           tree_cons (NULL_TREE, long_long_integer_type_node,
12301
                                      tree_cons (NULL_TREE, integer_type_node,
12302
                                                 tree_cons (NULL_TREE,
12303
                                                            integer_type_node,
12304
                                                            endlink))));
12305
  tree int_ftype_v8qi
12306
    = build_function_type (integer_type_node,
12307
                           tree_cons (NULL_TREE, V8QI_type_node,
12308
                                      endlink));
12309
  tree int_ftype_v4hi
12310
    = build_function_type (integer_type_node,
12311
                           tree_cons (NULL_TREE, V4HI_type_node,
12312
                                      endlink));
12313
  tree int_ftype_v2si
12314
    = build_function_type (integer_type_node,
12315
                           tree_cons (NULL_TREE, V2SI_type_node,
12316
                                      endlink));
12317
  tree int_ftype_v8qi_int
12318
    = build_function_type (integer_type_node,
12319
                           tree_cons (NULL_TREE, V8QI_type_node,
12320
                                      tree_cons (NULL_TREE, integer_type_node,
12321
                                                 endlink)));
12322
  tree int_ftype_v4hi_int
12323
    = build_function_type (integer_type_node,
12324
                           tree_cons (NULL_TREE, V4HI_type_node,
12325
                                      tree_cons (NULL_TREE, integer_type_node,
12326
                                                 endlink)));
12327
  tree int_ftype_v2si_int
12328
    = build_function_type (integer_type_node,
12329
                           tree_cons (NULL_TREE, V2SI_type_node,
12330
                                      tree_cons (NULL_TREE, integer_type_node,
12331
                                                 endlink)));
12332
  tree v8qi_ftype_v8qi_int_int
12333
    = build_function_type (V8QI_type_node,
12334
                           tree_cons (NULL_TREE, V8QI_type_node,
12335
                                      tree_cons (NULL_TREE, integer_type_node,
12336
                                                 tree_cons (NULL_TREE,
12337
                                                            integer_type_node,
12338
                                                            endlink))));
12339
  tree v4hi_ftype_v4hi_int_int
12340
    = build_function_type (V4HI_type_node,
12341
                           tree_cons (NULL_TREE, V4HI_type_node,
12342
                                      tree_cons (NULL_TREE, integer_type_node,
12343
                                                 tree_cons (NULL_TREE,
12344
                                                            integer_type_node,
12345
                                                            endlink))));
12346
  tree v2si_ftype_v2si_int_int
12347
    = build_function_type (V2SI_type_node,
12348
                           tree_cons (NULL_TREE, V2SI_type_node,
12349
                                      tree_cons (NULL_TREE, integer_type_node,
12350
                                                 tree_cons (NULL_TREE,
12351
                                                            integer_type_node,
12352
                                                            endlink))));
12353
  /* Miscellaneous.  */
12354
  tree v8qi_ftype_v4hi_v4hi
12355
    = build_function_type (V8QI_type_node,
12356
                           tree_cons (NULL_TREE, V4HI_type_node,
12357
                                      tree_cons (NULL_TREE, V4HI_type_node,
12358
                                                 endlink)));
12359
  tree v4hi_ftype_v2si_v2si
12360
    = build_function_type (V4HI_type_node,
12361
                           tree_cons (NULL_TREE, V2SI_type_node,
12362
                                      tree_cons (NULL_TREE, V2SI_type_node,
12363
                                                 endlink)));
12364
  tree v2si_ftype_v4hi_v4hi
12365
    = build_function_type (V2SI_type_node,
12366
                           tree_cons (NULL_TREE, V4HI_type_node,
12367
                                      tree_cons (NULL_TREE, V4HI_type_node,
12368
                                                 endlink)));
12369
  tree v2si_ftype_v8qi_v8qi
12370
    = build_function_type (V2SI_type_node,
12371
                           tree_cons (NULL_TREE, V8QI_type_node,
12372
                                      tree_cons (NULL_TREE, V8QI_type_node,
12373
                                                 endlink)));
12374
  tree v4hi_ftype_v4hi_di
12375
    = build_function_type (V4HI_type_node,
12376
                           tree_cons (NULL_TREE, V4HI_type_node,
12377
                                      tree_cons (NULL_TREE,
12378
                                                 long_long_integer_type_node,
12379
                                                 endlink)));
12380
  tree v2si_ftype_v2si_di
12381
    = build_function_type (V2SI_type_node,
12382
                           tree_cons (NULL_TREE, V2SI_type_node,
12383
                                      tree_cons (NULL_TREE,
12384
                                                 long_long_integer_type_node,
12385
                                                 endlink)));
12386
  tree void_ftype_int_int
12387
    = build_function_type (void_type_node,
12388
                           tree_cons (NULL_TREE, integer_type_node,
12389
                                      tree_cons (NULL_TREE, integer_type_node,
12390
                                                 endlink)));
12391
  tree di_ftype_void
12392
    = build_function_type (long_long_unsigned_type_node, endlink);
12393
  tree di_ftype_v8qi
12394
    = build_function_type (long_long_integer_type_node,
12395
                           tree_cons (NULL_TREE, V8QI_type_node,
12396
                                      endlink));
12397
  tree di_ftype_v4hi
12398
    = build_function_type (long_long_integer_type_node,
12399
                           tree_cons (NULL_TREE, V4HI_type_node,
12400
                                      endlink));
12401
  tree di_ftype_v2si
12402
    = build_function_type (long_long_integer_type_node,
12403
                           tree_cons (NULL_TREE, V2SI_type_node,
12404
                                      endlink));
12405
  tree v2si_ftype_v4hi
12406
    = build_function_type (V2SI_type_node,
12407
                           tree_cons (NULL_TREE, V4HI_type_node,
12408
                                      endlink));
12409
  tree v4hi_ftype_v8qi
12410
    = build_function_type (V4HI_type_node,
12411
                           tree_cons (NULL_TREE, V8QI_type_node,
12412
                                      endlink));
12413
 
12414
  tree di_ftype_di_v4hi_v4hi
12415
    = build_function_type (long_long_unsigned_type_node,
12416
                           tree_cons (NULL_TREE,
12417
                                      long_long_unsigned_type_node,
12418
                                      tree_cons (NULL_TREE, V4HI_type_node,
12419
                                                 tree_cons (NULL_TREE,
12420
                                                            V4HI_type_node,
12421
                                                            endlink))));
12422
 
12423
  tree di_ftype_v4hi_v4hi
12424
    = build_function_type (long_long_unsigned_type_node,
12425
                           tree_cons (NULL_TREE, V4HI_type_node,
12426
                                      tree_cons (NULL_TREE, V4HI_type_node,
12427
                                                 endlink)));
12428
 
12429
  /* Normal vector binops.  */
12430
  tree v8qi_ftype_v8qi_v8qi
12431
    = build_function_type (V8QI_type_node,
12432
                           tree_cons (NULL_TREE, V8QI_type_node,
12433
                                      tree_cons (NULL_TREE, V8QI_type_node,
12434
                                                 endlink)));
12435
  tree v4hi_ftype_v4hi_v4hi
12436
    = build_function_type (V4HI_type_node,
12437
                           tree_cons (NULL_TREE, V4HI_type_node,
12438
                                      tree_cons (NULL_TREE, V4HI_type_node,
12439
                                                 endlink)));
12440
  tree v2si_ftype_v2si_v2si
12441
    = build_function_type (V2SI_type_node,
12442
                           tree_cons (NULL_TREE, V2SI_type_node,
12443
                                      tree_cons (NULL_TREE, V2SI_type_node,
12444
                                                 endlink)));
12445
  tree di_ftype_di_di
12446
    = build_function_type (long_long_unsigned_type_node,
12447
                           tree_cons (NULL_TREE, long_long_unsigned_type_node,
12448
                                      tree_cons (NULL_TREE,
12449
                                                 long_long_unsigned_type_node,
12450
                                                 endlink)));
12451
 
12452
  /* Add all builtins that are more or less simple operations on two
12453
     operands.  */
12454
  for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12455
    {
12456
      /* Use one of the operands; the target can have a different mode for
12457
         mask-generating compares.  */
12458
      enum machine_mode mode;
12459
      tree type;
12460
 
12461
      if (d->name == 0)
12462
        continue;
12463
 
12464
      mode = insn_data[d->icode].operand[1].mode;
12465
 
12466
      switch (mode)
12467
        {
12468
        case V8QImode:
12469
          type = v8qi_ftype_v8qi_v8qi;
12470
          break;
12471
        case V4HImode:
12472
          type = v4hi_ftype_v4hi_v4hi;
12473
          break;
12474
        case V2SImode:
12475
          type = v2si_ftype_v2si_v2si;
12476
          break;
12477
        case DImode:
12478
          type = di_ftype_di_di;
12479
          break;
12480
 
12481
        default:
12482
          gcc_unreachable ();
12483
        }
12484
 
12485
      def_mbuiltin (d->mask, d->name, type, d->code);
12486
    }
12487
 
12488
  /* Add the remaining MMX insns with somewhat more complicated types.  */
12489
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
12490
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
12491
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
12492
 
12493
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
12494
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
12495
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
12496
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
12497
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
12498
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
12499
 
12500
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
12501
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
12502
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
12503
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
12504
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
12505
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
12506
 
12507
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
12508
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
12509
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
12510
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
12511
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
12512
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
12513
 
12514
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
12515
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
12516
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
12517
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
12518
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
12519
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
12520
 
12521
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12522
 
12523
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12524
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12525
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12526
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12527
 
12528
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12529
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12530
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12531
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12532
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12533
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12534
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12535
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12536
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12537
 
12538
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12539
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12540
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12541
 
12542
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12543
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12544
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12545
 
12546
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12547
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12548
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12549
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12550
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12551
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12552
 
12553
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12554
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12555
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12556
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12557
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12558
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12559
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12560
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12561
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12562
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12563
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12564
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12565
 
12566
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12567
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12568
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12569
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12570
 
12571
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12572
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12573
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12574
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12575
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12576
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12577
  def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12578
}
12579
 
12580
static void
12581
arm_init_tls_builtins (void)
12582
{
12583
  tree ftype;
12584
  tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
12585
  tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
12586
 
12587
  ftype = build_function_type (ptr_type_node, void_list_node);
12588
  lang_hooks.builtin_function ("__builtin_thread_pointer", ftype,
12589
                               ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
12590
                               NULL, const_nothrow);
12591
}
12592
 
12593
static void
12594
arm_init_builtins (void)
12595
{
12596
  arm_init_tls_builtins ();
12597
 
12598
  if (TARGET_REALLY_IWMMXT)
12599
    arm_init_iwmmxt_builtins ();
12600
}
12601
 
12602
/* Errors in the source file can cause expand_expr to return const0_rtx
12603
   where we expect a vector.  To avoid crashing, use one of the vector
12604
   clear instructions.  */
12605
 
12606
static rtx
12607
safe_vector_operand (rtx x, enum machine_mode mode)
12608
{
12609
  if (x != const0_rtx)
12610
    return x;
12611
  x = gen_reg_rtx (mode);
12612
 
12613
  emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12614
                               : gen_rtx_SUBREG (DImode, x, 0)));
12615
  return x;
12616
}
12617
 
12618
/* Subroutine of arm_expand_builtin to take care of binop insns.  */
12619
 
12620
static rtx
12621
arm_expand_binop_builtin (enum insn_code icode,
12622
                          tree arglist, rtx target)
12623
{
12624
  rtx pat;
12625
  tree arg0 = TREE_VALUE (arglist);
12626
  tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12627
  rtx op0 = expand_normal (arg0);
12628
  rtx op1 = expand_normal (arg1);
12629
  enum machine_mode tmode = insn_data[icode].operand[0].mode;
12630
  enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12631
  enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12632
 
12633
  if (VECTOR_MODE_P (mode0))
12634
    op0 = safe_vector_operand (op0, mode0);
12635
  if (VECTOR_MODE_P (mode1))
12636
    op1 = safe_vector_operand (op1, mode1);
12637
 
12638
  if (! target
12639
      || GET_MODE (target) != tmode
12640
      || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12641
    target = gen_reg_rtx (tmode);
12642
 
12643
  gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12644
 
12645
  if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12646
    op0 = copy_to_mode_reg (mode0, op0);
12647
  if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12648
    op1 = copy_to_mode_reg (mode1, op1);
12649
 
12650
  pat = GEN_FCN (icode) (target, op0, op1);
12651
  if (! pat)
12652
    return 0;
12653
  emit_insn (pat);
12654
  return target;
12655
}
12656
 
12657
/* Subroutine of arm_expand_builtin to take care of unop insns.  */
12658
 
12659
static rtx
12660
arm_expand_unop_builtin (enum insn_code icode,
12661
                         tree arglist, rtx target, int do_load)
12662
{
12663
  rtx pat;
12664
  tree arg0 = TREE_VALUE (arglist);
12665
  rtx op0 = expand_normal (arg0);
12666
  enum machine_mode tmode = insn_data[icode].operand[0].mode;
12667
  enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12668
 
12669
  if (! target
12670
      || GET_MODE (target) != tmode
12671
      || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12672
    target = gen_reg_rtx (tmode);
12673
  if (do_load)
12674
    op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12675
  else
12676
    {
12677
      if (VECTOR_MODE_P (mode0))
12678
        op0 = safe_vector_operand (op0, mode0);
12679
 
12680
      if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12681
        op0 = copy_to_mode_reg (mode0, op0);
12682
    }
12683
 
12684
  pat = GEN_FCN (icode) (target, op0);
12685
  if (! pat)
12686
    return 0;
12687
  emit_insn (pat);
12688
  return target;
12689
}
12690
 
12691
/* Expand an expression EXP that calls a built-in function,
12692
   with result going to TARGET if that's convenient
12693
   (and in mode MODE if that's convenient).
12694
   SUBTARGET may be used as the target for computing one of EXP's operands.
12695
   IGNORE is nonzero if the value is to be ignored.  */
12696
 
12697
static rtx
12698
arm_expand_builtin (tree exp,
12699
                    rtx target,
12700
                    rtx subtarget ATTRIBUTE_UNUSED,
12701
                    enum machine_mode mode ATTRIBUTE_UNUSED,
12702
                    int ignore ATTRIBUTE_UNUSED)
12703
{
12704
  const struct builtin_description * d;
12705
  enum insn_code    icode;
12706
  tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12707
  tree              arglist = TREE_OPERAND (exp, 1);
12708
  tree              arg0;
12709
  tree              arg1;
12710
  tree              arg2;
12711
  rtx               op0;
12712
  rtx               op1;
12713
  rtx               op2;
12714
  rtx               pat;
12715
  int               fcode = DECL_FUNCTION_CODE (fndecl);
12716
  size_t            i;
12717
  enum machine_mode tmode;
12718
  enum machine_mode mode0;
12719
  enum machine_mode mode1;
12720
  enum machine_mode mode2;
12721
 
12722
  switch (fcode)
12723
    {
12724
    case ARM_BUILTIN_TEXTRMSB:
12725
    case ARM_BUILTIN_TEXTRMUB:
12726
    case ARM_BUILTIN_TEXTRMSH:
12727
    case ARM_BUILTIN_TEXTRMUH:
12728
    case ARM_BUILTIN_TEXTRMSW:
12729
    case ARM_BUILTIN_TEXTRMUW:
12730
      icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12731
               : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12732
               : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12733
               : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12734
               : CODE_FOR_iwmmxt_textrmw);
12735
 
12736
      arg0 = TREE_VALUE (arglist);
12737
      arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12738
      op0 = expand_normal (arg0);
12739
      op1 = expand_normal (arg1);
12740
      tmode = insn_data[icode].operand[0].mode;
12741
      mode0 = insn_data[icode].operand[1].mode;
12742
      mode1 = insn_data[icode].operand[2].mode;
12743
 
12744
      if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12745
        op0 = copy_to_mode_reg (mode0, op0);
12746
      if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12747
        {
12748
          /* @@@ better error message */
12749
          error ("selector must be an immediate");
12750
          return gen_reg_rtx (tmode);
12751
        }
12752
      if (target == 0
12753
          || GET_MODE (target) != tmode
12754
          || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12755
        target = gen_reg_rtx (tmode);
12756
      pat = GEN_FCN (icode) (target, op0, op1);
12757
      if (! pat)
12758
        return 0;
12759
      emit_insn (pat);
12760
      return target;
12761
 
12762
    case ARM_BUILTIN_TINSRB:
12763
    case ARM_BUILTIN_TINSRH:
12764
    case ARM_BUILTIN_TINSRW:
12765
      icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12766
               : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12767
               : CODE_FOR_iwmmxt_tinsrw);
12768
      arg0 = TREE_VALUE (arglist);
12769
      arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12770
      arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12771
      op0 = expand_normal (arg0);
12772
      op1 = expand_normal (arg1);
12773
      op2 = expand_normal (arg2);
12774
      tmode = insn_data[icode].operand[0].mode;
12775
      mode0 = insn_data[icode].operand[1].mode;
12776
      mode1 = insn_data[icode].operand[2].mode;
12777
      mode2 = insn_data[icode].operand[3].mode;
12778
 
12779
      if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12780
        op0 = copy_to_mode_reg (mode0, op0);
12781
      if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12782
        op1 = copy_to_mode_reg (mode1, op1);
12783
      if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12784
        {
12785
          /* @@@ better error message */
12786
          error ("selector must be an immediate");
12787
          return const0_rtx;
12788
        }
12789
      if (target == 0
12790
          || GET_MODE (target) != tmode
12791
          || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12792
        target = gen_reg_rtx (tmode);
12793
      pat = GEN_FCN (icode) (target, op0, op1, op2);
12794
      if (! pat)
12795
        return 0;
12796
      emit_insn (pat);
12797
      return target;
12798
 
12799
    case ARM_BUILTIN_SETWCX:
12800
      arg0 = TREE_VALUE (arglist);
12801
      arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12802
      op0 = force_reg (SImode, expand_normal (arg0));
12803
      op1 = expand_normal (arg1);
12804
      emit_insn (gen_iwmmxt_tmcr (op1, op0));
12805
      return 0;
12806
 
12807
    case ARM_BUILTIN_GETWCX:
12808
      arg0 = TREE_VALUE (arglist);
12809
      op0 = expand_normal (arg0);
12810
      target = gen_reg_rtx (SImode);
12811
      emit_insn (gen_iwmmxt_tmrc (target, op0));
12812
      return target;
12813
 
12814
    case ARM_BUILTIN_WSHUFH:
12815
      icode = CODE_FOR_iwmmxt_wshufh;
12816
      arg0 = TREE_VALUE (arglist);
12817
      arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12818
      op0 = expand_normal (arg0);
12819
      op1 = expand_normal (arg1);
12820
      tmode = insn_data[icode].operand[0].mode;
12821
      mode1 = insn_data[icode].operand[1].mode;
12822
      mode2 = insn_data[icode].operand[2].mode;
12823
 
12824
      if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12825
        op0 = copy_to_mode_reg (mode1, op0);
12826
      if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12827
        {
12828
          /* @@@ better error message */
12829
          error ("mask must be an immediate");
12830
          return const0_rtx;
12831
        }
12832
      if (target == 0
12833
          || GET_MODE (target) != tmode
12834
          || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12835
        target = gen_reg_rtx (tmode);
12836
      pat = GEN_FCN (icode) (target, op0, op1);
12837
      if (! pat)
12838
        return 0;
12839
      emit_insn (pat);
12840
      return target;
12841
 
12842
    case ARM_BUILTIN_WSADB:
12843
      return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12844
    case ARM_BUILTIN_WSADH:
12845
      return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12846
    case ARM_BUILTIN_WSADBZ:
12847
      return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12848
    case ARM_BUILTIN_WSADHZ:
12849
      return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12850
 
12851
      /* Several three-argument builtins.  */
12852
    case ARM_BUILTIN_WMACS:
12853
    case ARM_BUILTIN_WMACU:
12854
    case ARM_BUILTIN_WALIGN:
12855
    case ARM_BUILTIN_TMIA:
12856
    case ARM_BUILTIN_TMIAPH:
12857
    case ARM_BUILTIN_TMIATT:
12858
    case ARM_BUILTIN_TMIATB:
12859
    case ARM_BUILTIN_TMIABT:
12860
    case ARM_BUILTIN_TMIABB:
12861
      icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12862
               : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12863
               : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12864
               : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12865
               : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12866
               : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12867
               : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12868
               : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12869
               : CODE_FOR_iwmmxt_walign);
12870
      arg0 = TREE_VALUE (arglist);
12871
      arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12872
      arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12873
      op0 = expand_normal (arg0);
12874
      op1 = expand_normal (arg1);
12875
      op2 = expand_normal (arg2);
12876
      tmode = insn_data[icode].operand[0].mode;
12877
      mode0 = insn_data[icode].operand[1].mode;
12878
      mode1 = insn_data[icode].operand[2].mode;
12879
      mode2 = insn_data[icode].operand[3].mode;
12880
 
12881
      if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12882
        op0 = copy_to_mode_reg (mode0, op0);
12883
      if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12884
        op1 = copy_to_mode_reg (mode1, op1);
12885
      if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12886
        op2 = copy_to_mode_reg (mode2, op2);
12887
      if (target == 0
12888
          || GET_MODE (target) != tmode
12889
          || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12890
        target = gen_reg_rtx (tmode);
12891
      pat = GEN_FCN (icode) (target, op0, op1, op2);
12892
      if (! pat)
12893
        return 0;
12894
      emit_insn (pat);
12895
      return target;
12896
 
12897
    case ARM_BUILTIN_WZERO:
12898
      target = gen_reg_rtx (DImode);
12899
      emit_insn (gen_iwmmxt_clrdi (target));
12900
      return target;
12901
 
12902
    case ARM_BUILTIN_THREAD_POINTER:
12903
      return arm_load_tp (target);
12904
 
12905
    default:
12906
      break;
12907
    }
12908
 
12909
  for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12910
    if (d->code == (const enum arm_builtins) fcode)
12911
      return arm_expand_binop_builtin (d->icode, arglist, target);
12912
 
12913
  for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12914
    if (d->code == (const enum arm_builtins) fcode)
12915
      return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12916
 
12917
  /* @@@ Should really do something sensible here.  */
12918
  return NULL_RTX;
12919
}
12920
 
12921
/* Return the number (counting from 0) of
12922
   the least significant set bit in MASK.  */
12923
 
12924
inline static int
12925
number_of_first_bit_set (unsigned mask)
12926
{
12927
  int bit;
12928
 
12929
  for (bit = 0;
12930
       (mask & (1 << bit)) == 0;
12931
       ++bit)
12932
    continue;
12933
 
12934
  return bit;
12935
}
12936
 
12937
/* Emit code to push or pop registers to or from the stack.  F is the
12938
   assembly file.  MASK is the registers to push or pop.  PUSH is
12939
   nonzero if we should push, and zero if we should pop.  For debugging
12940
   output, if pushing, adjust CFA_OFFSET by the amount of space added
12941
   to the stack.  REAL_REGS should have the same number of bits set as
12942
   MASK, and will be used instead (in the same order) to describe which
12943
   registers were saved - this is used to mark the save slots when we
12944
   push high registers after moving them to low registers.  */
12945
static void
12946
thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12947
               unsigned long real_regs)
12948
{
12949
  int regno;
12950
  int lo_mask = mask & 0xFF;
12951
  int pushed_words = 0;
12952
 
12953
  gcc_assert (mask);
12954
 
12955
  if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12956
    {
12957
      /* Special case.  Do not generate a POP PC statement here, do it in
12958
         thumb_exit() */
12959
      thumb_exit (f, -1);
12960
      return;
12961
    }
12962
 
12963
  if (ARM_EABI_UNWIND_TABLES && push)
12964
    {
12965
      fprintf (f, "\t.save\t{");
12966
      for (regno = 0; regno < 15; regno++)
12967
        {
12968
          if (real_regs & (1 << regno))
12969
            {
12970
              if (real_regs & ((1 << regno) -1))
12971
                fprintf (f, ", ");
12972
              asm_fprintf (f, "%r", regno);
12973
            }
12974
        }
12975
      fprintf (f, "}\n");
12976
    }
12977
 
12978
  fprintf (f, "\t%s\t{", push ? "push" : "pop");
12979
 
12980
  /* Look at the low registers first.  */
12981
  for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12982
    {
12983
      if (lo_mask & 1)
12984
        {
12985
          asm_fprintf (f, "%r", regno);
12986
 
12987
          if ((lo_mask & ~1) != 0)
12988
            fprintf (f, ", ");
12989
 
12990
          pushed_words++;
12991
        }
12992
    }
12993
 
12994
  if (push && (mask & (1 << LR_REGNUM)))
12995
    {
12996
      /* Catch pushing the LR.  */
12997
      if (mask & 0xFF)
12998
        fprintf (f, ", ");
12999
 
13000
      asm_fprintf (f, "%r", LR_REGNUM);
13001
 
13002
      pushed_words++;
13003
    }
13004
  else if (!push && (mask & (1 << PC_REGNUM)))
13005
    {
13006
      /* Catch popping the PC.  */
13007
      if (TARGET_INTERWORK || TARGET_BACKTRACE
13008
          || current_function_calls_eh_return)
13009
        {
13010
          /* The PC is never poped directly, instead
13011
             it is popped into r3 and then BX is used.  */
13012
          fprintf (f, "}\n");
13013
 
13014
          thumb_exit (f, -1);
13015
 
13016
          return;
13017
        }
13018
      else
13019
        {
13020
          if (mask & 0xFF)
13021
            fprintf (f, ", ");
13022
 
13023
          asm_fprintf (f, "%r", PC_REGNUM);
13024
        }
13025
    }
13026
 
13027
  fprintf (f, "}\n");
13028
 
13029
  if (push && pushed_words && dwarf2out_do_frame ())
13030
    {
13031
      char *l = dwarf2out_cfi_label ();
13032
      int pushed_mask = real_regs;
13033
 
13034
      *cfa_offset += pushed_words * 4;
13035
      dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
13036
 
13037
      pushed_words = 0;
13038
      pushed_mask = real_regs;
13039
      for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
13040
        {
13041
          if (pushed_mask & 1)
13042
            dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
13043
        }
13044
    }
13045
}
13046
 
13047
/* Generate code to return from a thumb function.
13048
   If 'reg_containing_return_addr' is -1, then the return address is
13049
   actually on the stack, at the stack pointer.  */
13050
static void
13051
thumb_exit (FILE *f, int reg_containing_return_addr)
13052
{
13053
  unsigned regs_available_for_popping;
13054
  unsigned regs_to_pop;
13055
  int pops_needed;
13056
  unsigned available;
13057
  unsigned required;
13058
  int mode;
13059
  int size;
13060
  int restore_a4 = FALSE;
13061
 
13062
  /* Compute the registers we need to pop.  */
13063
  regs_to_pop = 0;
13064
  pops_needed = 0;
13065
 
13066
  if (reg_containing_return_addr == -1)
13067
    {
13068
      regs_to_pop |= 1 << LR_REGNUM;
13069
      ++pops_needed;
13070
    }
13071
 
13072
  if (TARGET_BACKTRACE)
13073
    {
13074
      /* Restore the (ARM) frame pointer and stack pointer.  */
13075
      regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
13076
      pops_needed += 2;
13077
    }
13078
 
13079
  /* If there is nothing to pop then just emit the BX instruction and
13080
     return.  */
13081
  if (pops_needed == 0)
13082
    {
13083
      if (current_function_calls_eh_return)
13084
        asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13085
 
13086
      asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13087
      return;
13088
    }
13089
  /* Otherwise if we are not supporting interworking and we have not created
13090
     a backtrace structure and the function was not entered in ARM mode then
13091
     just pop the return address straight into the PC.  */
13092
  else if (!TARGET_INTERWORK
13093
           && !TARGET_BACKTRACE
13094
           && !is_called_in_ARM_mode (current_function_decl)
13095
           && !current_function_calls_eh_return)
13096
    {
13097
      asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
13098
      return;
13099
    }
13100
 
13101
  /* Find out how many of the (return) argument registers we can corrupt.  */
13102
  regs_available_for_popping = 0;
13103
 
13104
  /* If returning via __builtin_eh_return, the bottom three registers
13105
     all contain information needed for the return.  */
13106
  if (current_function_calls_eh_return)
13107
    size = 12;
13108
  else
13109
    {
13110
      /* If we can deduce the registers used from the function's
13111
         return value.  This is more reliable that examining
13112
         regs_ever_live[] because that will be set if the register is
13113
         ever used in the function, not just if the register is used
13114
         to hold a return value.  */
13115
 
13116
      if (current_function_return_rtx != 0)
13117
        mode = GET_MODE (current_function_return_rtx);
13118
      else
13119
        mode = DECL_MODE (DECL_RESULT (current_function_decl));
13120
 
13121
      size = GET_MODE_SIZE (mode);
13122
 
13123
      if (size == 0)
13124
        {
13125
          /* In a void function we can use any argument register.
13126
             In a function that returns a structure on the stack
13127
             we can use the second and third argument registers.  */
13128
          if (mode == VOIDmode)
13129
            regs_available_for_popping =
13130
              (1 << ARG_REGISTER (1))
13131
              | (1 << ARG_REGISTER (2))
13132
              | (1 << ARG_REGISTER (3));
13133
          else
13134
            regs_available_for_popping =
13135
              (1 << ARG_REGISTER (2))
13136
              | (1 << ARG_REGISTER (3));
13137
        }
13138
      else if (size <= 4)
13139
        regs_available_for_popping =
13140
          (1 << ARG_REGISTER (2))
13141
          | (1 << ARG_REGISTER (3));
13142
      else if (size <= 8)
13143
        regs_available_for_popping =
13144
          (1 << ARG_REGISTER (3));
13145
    }
13146
 
13147
  /* Match registers to be popped with registers into which we pop them.  */
13148
  for (available = regs_available_for_popping,
13149
       required  = regs_to_pop;
13150
       required != 0 && available != 0;
13151
       available &= ~(available & - available),
13152
       required  &= ~(required  & - required))
13153
    -- pops_needed;
13154
 
13155
  /* If we have any popping registers left over, remove them.  */
13156
  if (available > 0)
13157
    regs_available_for_popping &= ~available;
13158
 
13159
  /* Otherwise if we need another popping register we can use
13160
     the fourth argument register.  */
13161
  else if (pops_needed)
13162
    {
13163
      /* If we have not found any free argument registers and
13164
         reg a4 contains the return address, we must move it.  */
13165
      if (regs_available_for_popping == 0
13166
          && reg_containing_return_addr == LAST_ARG_REGNUM)
13167
        {
13168
          asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13169
          reg_containing_return_addr = LR_REGNUM;
13170
        }
13171
      else if (size > 12)
13172
        {
13173
          /* Register a4 is being used to hold part of the return value,
13174
             but we have dire need of a free, low register.  */
13175
          restore_a4 = TRUE;
13176
 
13177
          asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
13178
        }
13179
 
13180
      if (reg_containing_return_addr != LAST_ARG_REGNUM)
13181
        {
13182
          /* The fourth argument register is available.  */
13183
          regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
13184
 
13185
          --pops_needed;
13186
        }
13187
    }
13188
 
13189
  /* Pop as many registers as we can.  */
13190
  thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13191
                 regs_available_for_popping);
13192
 
13193
  /* Process the registers we popped.  */
13194
  if (reg_containing_return_addr == -1)
13195
    {
13196
      /* The return address was popped into the lowest numbered register.  */
13197
      regs_to_pop &= ~(1 << LR_REGNUM);
13198
 
13199
      reg_containing_return_addr =
13200
        number_of_first_bit_set (regs_available_for_popping);
13201
 
13202
      /* Remove this register for the mask of available registers, so that
13203
         the return address will not be corrupted by further pops.  */
13204
      regs_available_for_popping &= ~(1 << reg_containing_return_addr);
13205
    }
13206
 
13207
  /* If we popped other registers then handle them here.  */
13208
  if (regs_available_for_popping)
13209
    {
13210
      int frame_pointer;
13211
 
13212
      /* Work out which register currently contains the frame pointer.  */
13213
      frame_pointer = number_of_first_bit_set (regs_available_for_popping);
13214
 
13215
      /* Move it into the correct place.  */
13216
      asm_fprintf (f, "\tmov\t%r, %r\n",
13217
                   ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
13218
 
13219
      /* (Temporarily) remove it from the mask of popped registers.  */
13220
      regs_available_for_popping &= ~(1 << frame_pointer);
13221
      regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
13222
 
13223
      if (regs_available_for_popping)
13224
        {
13225
          int stack_pointer;
13226
 
13227
          /* We popped the stack pointer as well,
13228
             find the register that contains it.  */
13229
          stack_pointer = number_of_first_bit_set (regs_available_for_popping);
13230
 
13231
          /* Move it into the stack register.  */
13232
          asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
13233
 
13234
          /* At this point we have popped all necessary registers, so
13235
             do not worry about restoring regs_available_for_popping
13236
             to its correct value:
13237
 
13238
             assert (pops_needed == 0)
13239
             assert (regs_available_for_popping == (1 << frame_pointer))
13240
             assert (regs_to_pop == (1 << STACK_POINTER))  */
13241
        }
13242
      else
13243
        {
13244
          /* Since we have just move the popped value into the frame
13245
             pointer, the popping register is available for reuse, and
13246
             we know that we still have the stack pointer left to pop.  */
13247
          regs_available_for_popping |= (1 << frame_pointer);
13248
        }
13249
    }
13250
 
13251
  /* If we still have registers left on the stack, but we no longer have
13252
     any registers into which we can pop them, then we must move the return
13253
     address into the link register and make available the register that
13254
     contained it.  */
13255
  if (regs_available_for_popping == 0 && pops_needed > 0)
13256
    {
13257
      regs_available_for_popping |= 1 << reg_containing_return_addr;
13258
 
13259
      asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
13260
                   reg_containing_return_addr);
13261
 
13262
      reg_containing_return_addr = LR_REGNUM;
13263
    }
13264
 
13265
  /* If we have registers left on the stack then pop some more.
13266
     We know that at most we will want to pop FP and SP.  */
13267
  if (pops_needed > 0)
13268
    {
13269
      int  popped_into;
13270
      int  move_to;
13271
 
13272
      thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13273
                     regs_available_for_popping);
13274
 
13275
      /* We have popped either FP or SP.
13276
         Move whichever one it is into the correct register.  */
13277
      popped_into = number_of_first_bit_set (regs_available_for_popping);
13278
      move_to     = number_of_first_bit_set (regs_to_pop);
13279
 
13280
      asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
13281
 
13282
      regs_to_pop &= ~(1 << move_to);
13283
 
13284
      --pops_needed;
13285
    }
13286
 
13287
  /* If we still have not popped everything then we must have only
13288
     had one register available to us and we are now popping the SP.  */
13289
  if (pops_needed > 0)
13290
    {
13291
      int  popped_into;
13292
 
13293
      thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13294
                     regs_available_for_popping);
13295
 
13296
      popped_into = number_of_first_bit_set (regs_available_for_popping);
13297
 
13298
      asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
13299
      /*
13300
        assert (regs_to_pop == (1 << STACK_POINTER))
13301
        assert (pops_needed == 1)
13302
      */
13303
    }
13304
 
13305
  /* If necessary restore the a4 register.  */
13306
  if (restore_a4)
13307
    {
13308
      if (reg_containing_return_addr != LR_REGNUM)
13309
        {
13310
          asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13311
          reg_containing_return_addr = LR_REGNUM;
13312
        }
13313
 
13314
      asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13315
    }
13316
 
13317
  if (current_function_calls_eh_return)
13318
    asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13319
 
13320
  /* Return to caller.  */
13321
  asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13322
}
13323
 
13324
 
13325
void
13326
thumb_final_prescan_insn (rtx insn)
13327
{
13328
  if (flag_print_asm_name)
13329
    asm_fprintf (asm_out_file, "%@ 0x%04x\n",
13330
                 INSN_ADDRESSES (INSN_UID (insn)));
13331
}
13332
 
13333
int
13334
thumb_shiftable_const (unsigned HOST_WIDE_INT val)
13335
{
13336
  unsigned HOST_WIDE_INT mask = 0xff;
13337
  int i;
13338
 
13339
  if (val == 0) /* XXX */
13340
    return 0;
13341
 
13342
  for (i = 0; i < 25; i++)
13343
    if ((val & (mask << i)) == val)
13344
      return 1;
13345
 
13346
  return 0;
13347
}
13348
 
13349
/* Returns nonzero if the current function contains,
13350
   or might contain a far jump.  */
13351
static int
13352
thumb_far_jump_used_p (void)
13353
{
13354
  rtx insn;
13355
 
13356
  /* This test is only important for leaf functions.  */
13357
  /* assert (!leaf_function_p ()); */
13358
 
13359
  /* If we have already decided that far jumps may be used,
13360
     do not bother checking again, and always return true even if
13361
     it turns out that they are not being used.  Once we have made
13362
     the decision that far jumps are present (and that hence the link
13363
     register will be pushed onto the stack) we cannot go back on it.  */
13364
  if (cfun->machine->far_jump_used)
13365
    return 1;
13366
 
13367
  /* If this function is not being called from the prologue/epilogue
13368
     generation code then it must be being called from the
13369
     INITIAL_ELIMINATION_OFFSET macro.  */
13370
  if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
13371
    {
13372
      /* In this case we know that we are being asked about the elimination
13373
         of the arg pointer register.  If that register is not being used,
13374
         then there are no arguments on the stack, and we do not have to
13375
         worry that a far jump might force the prologue to push the link
13376
         register, changing the stack offsets.  In this case we can just
13377
         return false, since the presence of far jumps in the function will
13378
         not affect stack offsets.
13379
 
13380
         If the arg pointer is live (or if it was live, but has now been
13381
         eliminated and so set to dead) then we do have to test to see if
13382
         the function might contain a far jump.  This test can lead to some
13383
         false negatives, since before reload is completed, then length of
13384
         branch instructions is not known, so gcc defaults to returning their
13385
         longest length, which in turn sets the far jump attribute to true.
13386
 
13387
         A false negative will not result in bad code being generated, but it
13388
         will result in a needless push and pop of the link register.  We
13389
         hope that this does not occur too often.
13390
 
13391
         If we need doubleword stack alignment this could affect the other
13392
         elimination offsets so we can't risk getting it wrong.  */
13393
      if (regs_ever_live [ARG_POINTER_REGNUM])
13394
        cfun->machine->arg_pointer_live = 1;
13395
      else if (!cfun->machine->arg_pointer_live)
13396
        return 0;
13397
    }
13398
 
13399
  /* Check to see if the function contains a branch
13400
     insn with the far jump attribute set.  */
13401
  for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13402
    {
13403
      if (GET_CODE (insn) == JUMP_INSN
13404
          /* Ignore tablejump patterns.  */
13405
          && GET_CODE (PATTERN (insn)) != ADDR_VEC
13406
          && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
13407
          && get_attr_far_jump (insn) == FAR_JUMP_YES
13408
          )
13409
        {
13410
          /* Record the fact that we have decided that
13411
             the function does use far jumps.  */
13412
          cfun->machine->far_jump_used = 1;
13413
          return 1;
13414
        }
13415
    }
13416
 
13417
  return 0;
13418
}
13419
 
13420
/* Return nonzero if FUNC must be entered in ARM mode.  */
13421
int
13422
is_called_in_ARM_mode (tree func)
13423
{
13424
  gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
13425
 
13426
  /* Ignore the problem about functions whose address is taken.  */
13427
  if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
13428
    return TRUE;
13429
 
13430
#ifdef ARM_PE
13431
  return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
13432
#else
13433
  return FALSE;
13434
#endif
13435
}
13436
 
13437
/* The bits which aren't usefully expanded as rtl.  */
13438
const char *
13439
thumb_unexpanded_epilogue (void)
13440
{
13441
  int regno;
13442
  unsigned long live_regs_mask = 0;
13443
  int high_regs_pushed = 0;
13444
  int had_to_push_lr;
13445
  int size;
13446
 
13447
  if (return_used_this_function)
13448
    return "";
13449
 
13450
  if (IS_NAKED (arm_current_func_type ()))
13451
    return "";
13452
 
13453
  live_regs_mask = thumb_compute_save_reg_mask ();
13454
  high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13455
 
13456
  /* If we can deduce the registers used from the function's return value.
13457
     This is more reliable that examining regs_ever_live[] because that
13458
     will be set if the register is ever used in the function, not just if
13459
     the register is used to hold a return value.  */
13460
  size = arm_size_return_regs ();
13461
 
13462
  /* The prolog may have pushed some high registers to use as
13463
     work registers.  e.g. the testsuite file:
13464
     gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
13465
     compiles to produce:
13466
        push    {r4, r5, r6, r7, lr}
13467
        mov     r7, r9
13468
        mov     r6, r8
13469
        push    {r6, r7}
13470
     as part of the prolog.  We have to undo that pushing here.  */
13471
 
13472
  if (high_regs_pushed)
13473
    {
13474
      unsigned long mask = live_regs_mask & 0xff;
13475
      int next_hi_reg;
13476
 
13477
      /* The available low registers depend on the size of the value we are
13478
         returning.  */
13479
      if (size <= 12)
13480
        mask |=  1 << 3;
13481
      if (size <= 8)
13482
        mask |= 1 << 2;
13483
 
13484
      if (mask == 0)
13485
        /* Oh dear!  We have no low registers into which we can pop
13486
           high registers!  */
13487
        internal_error
13488
          ("no low registers available for popping high registers");
13489
 
13490
      for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
13491
        if (live_regs_mask & (1 << next_hi_reg))
13492
          break;
13493
 
13494
      while (high_regs_pushed)
13495
        {
13496
          /* Find lo register(s) into which the high register(s) can
13497
             be popped.  */
13498
          for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13499
            {
13500
              if (mask & (1 << regno))
13501
                high_regs_pushed--;
13502
              if (high_regs_pushed == 0)
13503
                break;
13504
            }
13505
 
13506
          mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
13507
 
13508
          /* Pop the values into the low register(s).  */
13509
          thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13510
 
13511
          /* Move the value(s) into the high registers.  */
13512
          for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13513
            {
13514
              if (mask & (1 << regno))
13515
                {
13516
                  asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13517
                               regno);
13518
 
13519
                  for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13520
                    if (live_regs_mask & (1 << next_hi_reg))
13521
                      break;
13522
                }
13523
            }
13524
        }
13525
      live_regs_mask &= ~0x0f00;
13526
    }
13527
 
13528
  had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
13529
  live_regs_mask &= 0xff;
13530
 
13531
  if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13532
    {
13533
      /* Pop the return address into the PC.  */
13534
      if (had_to_push_lr)
13535
        live_regs_mask |= 1 << PC_REGNUM;
13536
 
13537
      /* Either no argument registers were pushed or a backtrace
13538
         structure was created which includes an adjusted stack
13539
         pointer, so just pop everything.  */
13540
      if (live_regs_mask)
13541
        thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13542
                       live_regs_mask);
13543
 
13544
      /* We have either just popped the return address into the
13545
         PC or it is was kept in LR for the entire function.  */
13546
      if (!had_to_push_lr)
13547
        thumb_exit (asm_out_file, LR_REGNUM);
13548
    }
13549
  else
13550
    {
13551
      /* Pop everything but the return address.  */
13552
      if (live_regs_mask)
13553
        thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13554
                       live_regs_mask);
13555
 
13556
      if (had_to_push_lr)
13557
        {
13558
          if (size > 12)
13559
            {
13560
              /* We have no free low regs, so save one.  */
13561
              asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
13562
                           LAST_ARG_REGNUM);
13563
            }
13564
 
13565
          /* Get the return address into a temporary register.  */
13566
          thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13567
                         1 << LAST_ARG_REGNUM);
13568
 
13569
          if (size > 12)
13570
            {
13571
              /* Move the return address to lr.  */
13572
              asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13573
                           LAST_ARG_REGNUM);
13574
              /* Restore the low register.  */
13575
              asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13576
                           IP_REGNUM);
13577
              regno = LR_REGNUM;
13578
            }
13579
          else
13580
            regno = LAST_ARG_REGNUM;
13581
        }
13582
      else
13583
        regno = LR_REGNUM;
13584
 
13585
      /* Remove the argument registers that were pushed onto the stack.  */
13586
      asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13587
                   SP_REGNUM, SP_REGNUM,
13588
                   current_function_pretend_args_size);
13589
 
13590
      thumb_exit (asm_out_file, regno);
13591
    }
13592
 
13593
  return "";
13594
}
13595
 
13596
/* Functions to save and restore machine-specific function data.  */
13597
static struct machine_function *
13598
arm_init_machine_status (void)
13599
{
13600
  struct machine_function *machine;
13601
  machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13602
 
13603
#if ARM_FT_UNKNOWN != 0
13604
  machine->func_type = ARM_FT_UNKNOWN;
13605
#endif
13606
  return machine;
13607
}
13608
 
13609
/* Return an RTX indicating where the return address to the
13610
   calling function can be found.  */
13611
rtx
13612
arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13613
{
13614
  if (count != 0)
13615
    return NULL_RTX;
13616
 
13617
  return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13618
}
13619
 
13620
/* Do anything needed before RTL is emitted for each function.  */
13621
void
13622
arm_init_expanders (void)
13623
{
13624
  /* Arrange to initialize and mark the machine per-function status.  */
13625
  init_machine_status = arm_init_machine_status;
13626
 
13627
  /* This is to stop the combine pass optimizing away the alignment
13628
     adjustment of va_arg.  */
13629
  /* ??? It is claimed that this should not be necessary.  */
13630
  if (cfun)
13631
    mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13632
}
13633
 
13634
 
13635
/* Like arm_compute_initial_elimination offset.  Simpler because there
13636
   isn't an ABI specified frame pointer for Thumb.  Instead, we set it
13637
   to point at the base of the local variables after static stack
13638
   space for a function has been allocated.  */
13639
 
13640
HOST_WIDE_INT
13641
thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13642
{
13643
  arm_stack_offsets *offsets;
13644
 
13645
  offsets = arm_get_frame_offsets ();
13646
 
13647
  switch (from)
13648
    {
13649
    case ARG_POINTER_REGNUM:
13650
      switch (to)
13651
        {
13652
        case STACK_POINTER_REGNUM:
13653
          return offsets->outgoing_args - offsets->saved_args;
13654
 
13655
        case FRAME_POINTER_REGNUM:
13656
          return offsets->soft_frame - offsets->saved_args;
13657
 
13658
        case ARM_HARD_FRAME_POINTER_REGNUM:
13659
          return offsets->saved_regs - offsets->saved_args;
13660
 
13661
        case THUMB_HARD_FRAME_POINTER_REGNUM:
13662
          return offsets->locals_base - offsets->saved_args;
13663
 
13664
        default:
13665
          gcc_unreachable ();
13666
        }
13667
      break;
13668
 
13669
    case FRAME_POINTER_REGNUM:
13670
      switch (to)
13671
        {
13672
        case STACK_POINTER_REGNUM:
13673
          return offsets->outgoing_args - offsets->soft_frame;
13674
 
13675
        case ARM_HARD_FRAME_POINTER_REGNUM:
13676
          return offsets->saved_regs - offsets->soft_frame;
13677
 
13678
        case THUMB_HARD_FRAME_POINTER_REGNUM:
13679
          return offsets->locals_base - offsets->soft_frame;
13680
 
13681
        default:
13682
          gcc_unreachable ();
13683
        }
13684
      break;
13685
 
13686
    default:
13687
      gcc_unreachable ();
13688
    }
13689
}
13690
 
13691
 
13692
/* Generate the rest of a function's prologue.  */
13693
void
13694
thumb_expand_prologue (void)
13695
{
13696
  rtx insn, dwarf;
13697
 
13698
  HOST_WIDE_INT amount;
13699
  arm_stack_offsets *offsets;
13700
  unsigned long func_type;
13701
  int regno;
13702
  unsigned long live_regs_mask;
13703
 
13704
  func_type = arm_current_func_type ();
13705
 
13706
  /* Naked functions don't have prologues.  */
13707
  if (IS_NAKED (func_type))
13708
    return;
13709
 
13710
  if (IS_INTERRUPT (func_type))
13711
    {
13712
      error ("interrupt Service Routines cannot be coded in Thumb mode");
13713
      return;
13714
    }
13715
 
13716
  live_regs_mask = thumb_compute_save_reg_mask ();
13717
  /* Load the pic register before setting the frame pointer,
13718
     so we can use r7 as a temporary work register.  */
13719
  if (flag_pic && arm_pic_register != INVALID_REGNUM)
13720
    arm_load_pic_register (live_regs_mask);
13721
 
13722
  if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13723
    emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13724
                    stack_pointer_rtx);
13725
 
13726
  offsets = arm_get_frame_offsets ();
13727
  amount = offsets->outgoing_args - offsets->saved_regs;
13728
  if (amount)
13729
    {
13730
      if (amount < 512)
13731
        {
13732
          insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13733
                                        GEN_INT (- amount)));
13734
          RTX_FRAME_RELATED_P (insn) = 1;
13735
        }
13736
      else
13737
        {
13738
          rtx reg;
13739
 
13740
          /* The stack decrement is too big for an immediate value in a single
13741
             insn.  In theory we could issue multiple subtracts, but after
13742
             three of them it becomes more space efficient to place the full
13743
             value in the constant pool and load into a register.  (Also the
13744
             ARM debugger really likes to see only one stack decrement per
13745
             function).  So instead we look for a scratch register into which
13746
             we can load the decrement, and then we subtract this from the
13747
             stack pointer.  Unfortunately on the thumb the only available
13748
             scratch registers are the argument registers, and we cannot use
13749
             these as they may hold arguments to the function.  Instead we
13750
             attempt to locate a call preserved register which is used by this
13751
             function.  If we can find one, then we know that it will have
13752
             been pushed at the start of the prologue and so we can corrupt
13753
             it now.  */
13754
          for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13755
            if (live_regs_mask & (1 << regno)
13756
                && !(frame_pointer_needed
13757
                     && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13758
              break;
13759
 
13760
          if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13761
            {
13762
              rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13763
 
13764
              /* Choose an arbitrary, non-argument low register.  */
13765
              reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13766
 
13767
              /* Save it by copying it into a high, scratch register.  */
13768
              emit_insn (gen_movsi (spare, reg));
13769
              /* Add a USE to stop propagate_one_insn() from barfing.  */
13770
              emit_insn (gen_prologue_use (spare));
13771
 
13772
              /* Decrement the stack.  */
13773
              emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13774
              insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13775
                                            stack_pointer_rtx, reg));
13776
              RTX_FRAME_RELATED_P (insn) = 1;
13777
              dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13778
                                   plus_constant (stack_pointer_rtx,
13779
                                                  -amount));
13780
              RTX_FRAME_RELATED_P (dwarf) = 1;
13781
              REG_NOTES (insn)
13782
                = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13783
                                     REG_NOTES (insn));
13784
 
13785
              /* Restore the low register's original value.  */
13786
              emit_insn (gen_movsi (reg, spare));
13787
 
13788
              /* Emit a USE of the restored scratch register, so that flow
13789
                 analysis will not consider the restore redundant.  The
13790
                 register won't be used again in this function and isn't
13791
                 restored by the epilogue.  */
13792
              emit_insn (gen_prologue_use (reg));
13793
            }
13794
          else
13795
            {
13796
              reg = gen_rtx_REG (SImode, regno);
13797
 
13798
              emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13799
 
13800
              insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13801
                                            stack_pointer_rtx, reg));
13802
              RTX_FRAME_RELATED_P (insn) = 1;
13803
              dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13804
                                   plus_constant (stack_pointer_rtx,
13805
                                                  -amount));
13806
              RTX_FRAME_RELATED_P (dwarf) = 1;
13807
              REG_NOTES (insn)
13808
                = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13809
                                     REG_NOTES (insn));
13810
            }
13811
        }
13812
    }
13813
 
13814
  if (frame_pointer_needed)
13815
    {
13816
      amount = offsets->outgoing_args - offsets->locals_base;
13817
 
13818
      if (amount < 1024)
13819
        insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13820
                                      stack_pointer_rtx, GEN_INT (amount)));
13821
      else
13822
        {
13823
          emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
13824
          insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13825
                                        hard_frame_pointer_rtx,
13826
                                        stack_pointer_rtx));
13827
          dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
13828
                               plus_constant (stack_pointer_rtx, amount));
13829
          RTX_FRAME_RELATED_P (dwarf) = 1;
13830
          REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13831
                                                REG_NOTES (insn));
13832
        }
13833
 
13834
      RTX_FRAME_RELATED_P (insn) = 1;
13835
    }
13836
 
13837
  /* If we are profiling, make sure no instructions are scheduled before
13838
     the call to mcount.  Similarly if the user has requested no
13839
     scheduling in the prolog.  Similarly if we want non-call exceptions
13840
     using the EABI unwinder, to prevent faulting instructions from being
13841
     swapped with a stack adjustment.  */
13842
  if (current_function_profile || !TARGET_SCHED_PROLOG
13843
      || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13844
    emit_insn (gen_blockage ());
13845
 
13846
  cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13847
  if (live_regs_mask & 0xff)
13848
    cfun->machine->lr_save_eliminated = 0;
13849
 
13850
  /* If the link register is being kept alive, with the return address in it,
13851
     then make sure that it does not get reused by the ce2 pass.  */
13852
  if (cfun->machine->lr_save_eliminated)
13853
    emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13854
}
13855
 
13856
 
13857
void
13858
thumb_expand_epilogue (void)
13859
{
13860
  HOST_WIDE_INT amount;
13861
  arm_stack_offsets *offsets;
13862
  int regno;
13863
 
13864
  /* Naked functions don't have prologues.  */
13865
  if (IS_NAKED (arm_current_func_type ()))
13866
    return;
13867
 
13868
  offsets = arm_get_frame_offsets ();
13869
  amount = offsets->outgoing_args - offsets->saved_regs;
13870
 
13871
  if (frame_pointer_needed)
13872
    {
13873
      emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13874
      amount = offsets->locals_base - offsets->saved_regs;
13875
    }
13876
 
13877
  gcc_assert (amount >= 0);
13878
  if (amount)
13879
    {
13880
      if (amount < 512)
13881
        emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13882
                               GEN_INT (amount)));
13883
      else
13884
        {
13885
          /* r3 is always free in the epilogue.  */
13886
          rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13887
 
13888
          emit_insn (gen_movsi (reg, GEN_INT (amount)));
13889
          emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13890
        }
13891
    }
13892
 
13893
  /* Emit a USE (stack_pointer_rtx), so that
13894
     the stack adjustment will not be deleted.  */
13895
  emit_insn (gen_prologue_use (stack_pointer_rtx));
13896
 
13897
  if (current_function_profile || !TARGET_SCHED_PROLOG)
13898
    emit_insn (gen_blockage ());
13899
 
13900
  /* Emit a clobber for each insn that will be restored in the epilogue,
13901
     so that flow2 will get register lifetimes correct.  */
13902
  for (regno = 0; regno < 13; regno++)
13903
    if (regs_ever_live[regno] && !call_used_regs[regno])
13904
      emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13905
 
13906
  if (! regs_ever_live[LR_REGNUM])
13907
    emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13908
}
13909
 
13910
static void
13911
thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13912
{
13913
  unsigned long live_regs_mask = 0;
13914
  unsigned long l_mask;
13915
  unsigned high_regs_pushed = 0;
13916
  int cfa_offset = 0;
13917
  int regno;
13918
 
13919
  if (IS_NAKED (arm_current_func_type ()))
13920
    return;
13921
 
13922
  if (is_called_in_ARM_mode (current_function_decl))
13923
    {
13924
      const char * name;
13925
 
13926
      gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13927
      gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13928
                  == SYMBOL_REF);
13929
      name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13930
 
13931
      /* Generate code sequence to switch us into Thumb mode.  */
13932
      /* The .code 32 directive has already been emitted by
13933
         ASM_DECLARE_FUNCTION_NAME.  */
13934
      asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13935
      asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13936
 
13937
      /* Generate a label, so that the debugger will notice the
13938
         change in instruction sets.  This label is also used by
13939
         the assembler to bypass the ARM code when this function
13940
         is called from a Thumb encoded function elsewhere in the
13941
         same file.  Hence the definition of STUB_NAME here must
13942
         agree with the definition in gas/config/tc-arm.c.  */
13943
 
13944
#define STUB_NAME ".real_start_of"
13945
 
13946
      fprintf (f, "\t.code\t16\n");
13947
#ifdef ARM_PE
13948
      if (arm_dllexport_name_p (name))
13949
        name = arm_strip_name_encoding (name);
13950
#endif
13951
      asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13952
      fprintf (f, "\t.thumb_func\n");
13953
      asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13954
    }
13955
 
13956
  if (current_function_pretend_args_size)
13957
    {
13958
      /* Output unwind directive for the stack adjustment.  */
13959
      if (ARM_EABI_UNWIND_TABLES)
13960
        fprintf (f, "\t.pad #%d\n",
13961
                 current_function_pretend_args_size);
13962
 
13963
      if (cfun->machine->uses_anonymous_args)
13964
        {
13965
          int num_pushes;
13966
 
13967
          fprintf (f, "\tpush\t{");
13968
 
13969
          num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13970
 
13971
          for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13972
               regno <= LAST_ARG_REGNUM;
13973
               regno++)
13974
            asm_fprintf (f, "%r%s", regno,
13975
                         regno == LAST_ARG_REGNUM ? "" : ", ");
13976
 
13977
          fprintf (f, "}\n");
13978
        }
13979
      else
13980
        asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13981
                     SP_REGNUM, SP_REGNUM,
13982
                     current_function_pretend_args_size);
13983
 
13984
      /* We don't need to record the stores for unwinding (would it
13985
         help the debugger any if we did?), but record the change in
13986
         the stack pointer.  */
13987
      if (dwarf2out_do_frame ())
13988
        {
13989
          char *l = dwarf2out_cfi_label ();
13990
 
13991
          cfa_offset = cfa_offset + current_function_pretend_args_size;
13992
          dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13993
        }
13994
    }
13995
 
13996
  /* Get the registers we are going to push.  */
13997
  live_regs_mask = thumb_compute_save_reg_mask ();
13998
  /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
13999
  l_mask = live_regs_mask & 0x40ff;
14000
  /* Then count how many other high registers will need to be pushed.  */
14001
  high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
14002
 
14003
  if (TARGET_BACKTRACE)
14004
    {
14005
      unsigned offset;
14006
      unsigned work_register;
14007
 
14008
      /* We have been asked to create a stack backtrace structure.
14009
         The code looks like this:
14010
 
14011
 
14012
 
14013
 
14014
         2     push  {R7}            Push low registers.
14015
         4     add   R7, SP, #20     Get the stack pointer before the push.
14016
         6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
14017
         8     mov   R7, PC          Get hold of the start of this code plus 12.
14018
        10     str   R7, [SP, #16]   Store it.
14019
        12     mov   R7, FP          Get hold of the current frame pointer.
14020
        14     str   R7, [SP, #4]    Store it.
14021
        16     mov   R7, LR          Get hold of the current return address.
14022
        18     str   R7, [SP, #12]   Store it.
14023
        20     add   R7, SP, #16     Point at the start of the backtrace structure.
14024
        22     mov   FP, R7          Put this value into the frame pointer.  */
14025
 
14026
      work_register = thumb_find_work_register (live_regs_mask);
14027
 
14028
      if (ARM_EABI_UNWIND_TABLES)
14029
        asm_fprintf (f, "\t.pad #16\n");
14030
 
14031
      asm_fprintf
14032
        (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
14033
         SP_REGNUM, SP_REGNUM);
14034
 
14035
      if (dwarf2out_do_frame ())
14036
        {
14037
          char *l = dwarf2out_cfi_label ();
14038
 
14039
          cfa_offset = cfa_offset + 16;
14040
          dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
14041
        }
14042
 
14043
      if (l_mask)
14044
        {
14045
          thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14046
          offset = bit_count (l_mask) * UNITS_PER_WORD;
14047
        }
14048
      else
14049
        offset = 0;
14050
 
14051
      asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14052
                   offset + 16 + current_function_pretend_args_size);
14053
 
14054
      asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14055
                   offset + 4);
14056
 
14057
      /* Make sure that the instruction fetching the PC is in the right place
14058
         to calculate "start of backtrace creation code + 12".  */
14059
      if (l_mask)
14060
        {
14061
          asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
14062
          asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14063
                       offset + 12);
14064
          asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14065
                       ARM_HARD_FRAME_POINTER_REGNUM);
14066
          asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14067
                       offset);
14068
        }
14069
      else
14070
        {
14071
          asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14072
                       ARM_HARD_FRAME_POINTER_REGNUM);
14073
          asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14074
                       offset);
14075
          asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
14076
          asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14077
                       offset + 12);
14078
        }
14079
 
14080
      asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
14081
      asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14082
                   offset + 8);
14083
      asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14084
                   offset + 12);
14085
      asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
14086
                   ARM_HARD_FRAME_POINTER_REGNUM, work_register);
14087
    }
14088
  /* Optimization:  If we are not pushing any low registers but we are going
14089
     to push some high registers then delay our first push.  This will just
14090
     be a push of LR and we can combine it with the push of the first high
14091
     register.  */
14092
  else if ((l_mask & 0xff) != 0
14093
           || (high_regs_pushed == 0 && l_mask))
14094
    thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14095
 
14096
  if (high_regs_pushed)
14097
    {
14098
      unsigned pushable_regs;
14099
      unsigned next_hi_reg;
14100
 
14101
      for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
14102
        if (live_regs_mask & (1 << next_hi_reg))
14103
          break;
14104
 
14105
      pushable_regs = l_mask & 0xff;
14106
 
14107
      if (pushable_regs == 0)
14108
        pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
14109
 
14110
      while (high_regs_pushed > 0)
14111
        {
14112
          unsigned long real_regs_mask = 0;
14113
 
14114
          for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
14115
            {
14116
              if (pushable_regs & (1 << regno))
14117
                {
14118
                  asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
14119
 
14120
                  high_regs_pushed --;
14121
                  real_regs_mask |= (1 << next_hi_reg);
14122
 
14123
                  if (high_regs_pushed)
14124
                    {
14125
                      for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
14126
                           next_hi_reg --)
14127
                        if (live_regs_mask & (1 << next_hi_reg))
14128
                          break;
14129
                    }
14130
                  else
14131
                    {
14132
                      pushable_regs &= ~((1 << regno) - 1);
14133
                      break;
14134
                    }
14135
                }
14136
            }
14137
 
14138
          /* If we had to find a work register and we have not yet
14139
             saved the LR then add it to the list of regs to push.  */
14140
          if (l_mask == (1 << LR_REGNUM))
14141
            {
14142
              thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
14143
                             1, &cfa_offset,
14144
                             real_regs_mask | (1 << LR_REGNUM));
14145
              l_mask = 0;
14146
            }
14147
          else
14148
            thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
14149
        }
14150
    }
14151
}
14152
 
14153
/* Handle the case of a double word load into a low register from
14154
   a computed memory address.  The computed address may involve a
14155
   register which is overwritten by the load.  */
14156
const char *
14157
thumb_load_double_from_address (rtx *operands)
14158
{
14159
  rtx addr;
14160
  rtx base;
14161
  rtx offset;
14162
  rtx arg1;
14163
  rtx arg2;
14164
 
14165
  gcc_assert (GET_CODE (operands[0]) == REG);
14166
  gcc_assert (GET_CODE (operands[1]) == MEM);
14167
 
14168
  /* Get the memory address.  */
14169
  addr = XEXP (operands[1], 0);
14170
 
14171
  /* Work out how the memory address is computed.  */
14172
  switch (GET_CODE (addr))
14173
    {
14174
    case REG:
14175
      operands[2] = adjust_address (operands[1], SImode, 4);
14176
 
14177
      if (REGNO (operands[0]) == REGNO (addr))
14178
        {
14179
          output_asm_insn ("ldr\t%H0, %2", operands);
14180
          output_asm_insn ("ldr\t%0, %1", operands);
14181
        }
14182
      else
14183
        {
14184
          output_asm_insn ("ldr\t%0, %1", operands);
14185
          output_asm_insn ("ldr\t%H0, %2", operands);
14186
        }
14187
      break;
14188
 
14189
    case CONST:
14190
      /* Compute <address> + 4 for the high order load.  */
14191
      operands[2] = adjust_address (operands[1], SImode, 4);
14192
 
14193
      output_asm_insn ("ldr\t%0, %1", operands);
14194
      output_asm_insn ("ldr\t%H0, %2", operands);
14195
      break;
14196
 
14197
    case PLUS:
14198
      arg1   = XEXP (addr, 0);
14199
      arg2   = XEXP (addr, 1);
14200
 
14201
      if (CONSTANT_P (arg1))
14202
        base = arg2, offset = arg1;
14203
      else
14204
        base = arg1, offset = arg2;
14205
 
14206
      gcc_assert (GET_CODE (base) == REG);
14207
 
14208
      /* Catch the case of <address> = <reg> + <reg> */
14209
      if (GET_CODE (offset) == REG)
14210
        {
14211
          int reg_offset = REGNO (offset);
14212
          int reg_base   = REGNO (base);
14213
          int reg_dest   = REGNO (operands[0]);
14214
 
14215
          /* Add the base and offset registers together into the
14216
             higher destination register.  */
14217
          asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
14218
                       reg_dest + 1, reg_base, reg_offset);
14219
 
14220
          /* Load the lower destination register from the address in
14221
             the higher destination register.  */
14222
          asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
14223
                       reg_dest, reg_dest + 1);
14224
 
14225
          /* Load the higher destination register from its own address
14226
             plus 4.  */
14227
          asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
14228
                       reg_dest + 1, reg_dest + 1);
14229
        }
14230
      else
14231
        {
14232
          /* Compute <address> + 4 for the high order load.  */
14233
          operands[2] = adjust_address (operands[1], SImode, 4);
14234
 
14235
          /* If the computed address is held in the low order register
14236
             then load the high order register first, otherwise always
14237
             load the low order register first.  */
14238
          if (REGNO (operands[0]) == REGNO (base))
14239
            {
14240
              output_asm_insn ("ldr\t%H0, %2", operands);
14241
              output_asm_insn ("ldr\t%0, %1", operands);
14242
            }
14243
          else
14244
            {
14245
              output_asm_insn ("ldr\t%0, %1", operands);
14246
              output_asm_insn ("ldr\t%H0, %2", operands);
14247
            }
14248
        }
14249
      break;
14250
 
14251
    case LABEL_REF:
14252
      /* With no registers to worry about we can just load the value
14253
         directly.  */
14254
      operands[2] = adjust_address (operands[1], SImode, 4);
14255
 
14256
      output_asm_insn ("ldr\t%H0, %2", operands);
14257
      output_asm_insn ("ldr\t%0, %1", operands);
14258
      break;
14259
 
14260
    default:
14261
      gcc_unreachable ();
14262
    }
14263
 
14264
  return "";
14265
}
14266
 
14267
const char *
14268
thumb_output_move_mem_multiple (int n, rtx *operands)
14269
{
14270
  rtx tmp;
14271
 
14272
  switch (n)
14273
    {
14274
    case 2:
14275
      if (REGNO (operands[4]) > REGNO (operands[5]))
14276
        {
14277
          tmp = operands[4];
14278
          operands[4] = operands[5];
14279
          operands[5] = tmp;
14280
        }
14281
      output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
14282
      output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
14283
      break;
14284
 
14285
    case 3:
14286
      if (REGNO (operands[4]) > REGNO (operands[5]))
14287
        {
14288
          tmp = operands[4];
14289
          operands[4] = operands[5];
14290
          operands[5] = tmp;
14291
        }
14292
      if (REGNO (operands[5]) > REGNO (operands[6]))
14293
        {
14294
          tmp = operands[5];
14295
          operands[5] = operands[6];
14296
          operands[6] = tmp;
14297
        }
14298
      if (REGNO (operands[4]) > REGNO (operands[5]))
14299
        {
14300
          tmp = operands[4];
14301
          operands[4] = operands[5];
14302
          operands[5] = tmp;
14303
        }
14304
 
14305
      output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
14306
      output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
14307
      break;
14308
 
14309
    default:
14310
      gcc_unreachable ();
14311
    }
14312
 
14313
  return "";
14314
}
14315
 
14316
/* Output a call-via instruction for thumb state.  */
14317
const char *
14318
thumb_call_via_reg (rtx reg)
14319
{
14320
  int regno = REGNO (reg);
14321
  rtx *labelp;
14322
 
14323
  gcc_assert (regno < LR_REGNUM);
14324
 
14325
  /* If we are in the normal text section we can use a single instance
14326
     per compilation unit.  If we are doing function sections, then we need
14327
     an entry per section, since we can't rely on reachability.  */
14328
  if (in_section == text_section)
14329
    {
14330
      thumb_call_reg_needed = 1;
14331
 
14332
      if (thumb_call_via_label[regno] == NULL)
14333
        thumb_call_via_label[regno] = gen_label_rtx ();
14334
      labelp = thumb_call_via_label + regno;
14335
    }
14336
  else
14337
    {
14338
      if (cfun->machine->call_via[regno] == NULL)
14339
        cfun->machine->call_via[regno] = gen_label_rtx ();
14340
      labelp = cfun->machine->call_via + regno;
14341
    }
14342
 
14343
  output_asm_insn ("bl\t%a0", labelp);
14344
  return "";
14345
}
14346
 
14347
/* Routines for generating rtl.  */
14348
void
14349
thumb_expand_movmemqi (rtx *operands)
14350
{
14351
  rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
14352
  rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
14353
  HOST_WIDE_INT len = INTVAL (operands[2]);
14354
  HOST_WIDE_INT offset = 0;
14355
 
14356
  while (len >= 12)
14357
    {
14358
      emit_insn (gen_movmem12b (out, in, out, in));
14359
      len -= 12;
14360
    }
14361
 
14362
  if (len >= 8)
14363
    {
14364
      emit_insn (gen_movmem8b (out, in, out, in));
14365
      len -= 8;
14366
    }
14367
 
14368
  if (len >= 4)
14369
    {
14370
      rtx reg = gen_reg_rtx (SImode);
14371
      emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
14372
      emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
14373
      len -= 4;
14374
      offset += 4;
14375
    }
14376
 
14377
  if (len >= 2)
14378
    {
14379
      rtx reg = gen_reg_rtx (HImode);
14380
      emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
14381
                                              plus_constant (in, offset))));
14382
      emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
14383
                            reg));
14384
      len -= 2;
14385
      offset += 2;
14386
    }
14387
 
14388
  if (len)
14389
    {
14390
      rtx reg = gen_reg_rtx (QImode);
14391
      emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
14392
                                              plus_constant (in, offset))));
14393
      emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
14394
                            reg));
14395
    }
14396
}
14397
 
14398
void
14399
thumb_reload_out_hi (rtx *operands)
14400
{
14401
  emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
14402
}
14403
 
14404
/* Handle reading a half-word from memory during reload.  */
14405
void
14406
thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
14407
{
14408
  gcc_unreachable ();
14409
}
14410
 
14411
/* Return the length of a function name prefix
14412
    that starts with the character 'c'.  */
14413
static int
14414
arm_get_strip_length (int c)
14415
{
14416
  switch (c)
14417
    {
14418
    ARM_NAME_ENCODING_LENGTHS
14419
      default: return 0;
14420
    }
14421
}
14422
 
14423
/* Return a pointer to a function's name with any
14424
   and all prefix encodings stripped from it.  */
14425
const char *
14426
arm_strip_name_encoding (const char *name)
14427
{
14428
  int skip;
14429
 
14430
  while ((skip = arm_get_strip_length (* name)))
14431
    name += skip;
14432
 
14433
  return name;
14434
}
14435
 
14436
/* If there is a '*' anywhere in the name's prefix, then
14437
   emit the stripped name verbatim, otherwise prepend an
14438
   underscore if leading underscores are being used.  */
14439
void
14440
arm_asm_output_labelref (FILE *stream, const char *name)
14441
{
14442
  int skip;
14443
  int verbatim = 0;
14444
 
14445
  while ((skip = arm_get_strip_length (* name)))
14446
    {
14447
      verbatim |= (*name == '*');
14448
      name += skip;
14449
    }
14450
 
14451
  if (verbatim)
14452
    fputs (name, stream);
14453
  else
14454
    asm_fprintf (stream, "%U%s", name);
14455
}
14456
 
14457
static void
14458
arm_file_end (void)
14459
{
14460
  int regno;
14461
 
14462
  if (! thumb_call_reg_needed)
14463
    return;
14464
 
14465
  switch_to_section (text_section);
14466
  asm_fprintf (asm_out_file, "\t.code 16\n");
14467
  ASM_OUTPUT_ALIGN (asm_out_file, 1);
14468
 
14469
  for (regno = 0; regno < LR_REGNUM; regno++)
14470
    {
14471
      rtx label = thumb_call_via_label[regno];
14472
 
14473
      if (label != 0)
14474
        {
14475
          targetm.asm_out.internal_label (asm_out_file, "L",
14476
                                          CODE_LABEL_NUMBER (label));
14477
          asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14478
        }
14479
    }
14480
}
14481
 
14482
rtx aof_pic_label;
14483
 
14484
#ifdef AOF_ASSEMBLER
14485
/* Special functions only needed when producing AOF syntax assembler.  */
14486
 
14487
struct pic_chain
14488
{
14489
  struct pic_chain * next;
14490
  const char * symname;
14491
};
14492
 
14493
static struct pic_chain * aof_pic_chain = NULL;
14494
 
14495
rtx
14496
aof_pic_entry (rtx x)
14497
{
14498
  struct pic_chain ** chainp;
14499
  int offset;
14500
 
14501
  if (aof_pic_label == NULL_RTX)
14502
    {
14503
      aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
14504
    }
14505
 
14506
  for (offset = 0, chainp = &aof_pic_chain; *chainp;
14507
       offset += 4, chainp = &(*chainp)->next)
14508
    if ((*chainp)->symname == XSTR (x, 0))
14509
      return plus_constant (aof_pic_label, offset);
14510
 
14511
  *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
14512
  (*chainp)->next = NULL;
14513
  (*chainp)->symname = XSTR (x, 0);
14514
  return plus_constant (aof_pic_label, offset);
14515
}
14516
 
14517
void
14518
aof_dump_pic_table (FILE *f)
14519
{
14520
  struct pic_chain * chain;
14521
 
14522
  if (aof_pic_chain == NULL)
14523
    return;
14524
 
14525
  asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
14526
               PIC_OFFSET_TABLE_REGNUM,
14527
               PIC_OFFSET_TABLE_REGNUM);
14528
  fputs ("|x$adcons|\n", f);
14529
 
14530
  for (chain = aof_pic_chain; chain; chain = chain->next)
14531
    {
14532
      fputs ("\tDCD\t", f);
14533
      assemble_name (f, chain->symname);
14534
      fputs ("\n", f);
14535
    }
14536
}
14537
 
14538
int arm_text_section_count = 1;
14539
 
14540
/* A get_unnamed_section callback for switching to the text section.  */
14541
 
14542
static void
14543
aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14544
{
14545
  fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
14546
           arm_text_section_count++);
14547
  if (flag_pic)
14548
    fprintf (asm_out_file, ", PIC, REENTRANT");
14549
  fprintf (asm_out_file, "\n");
14550
}
14551
 
14552
static int arm_data_section_count = 1;
14553
 
14554
/* A get_unnamed_section callback for switching to the data section.  */
14555
 
14556
static void
14557
aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14558
{
14559
  fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
14560
           arm_data_section_count++);
14561
}
14562
 
14563
/* Implement TARGET_ASM_INIT_SECTIONS.
14564
 
14565
   AOF Assembler syntax is a nightmare when it comes to areas, since once
14566
   we change from one area to another, we can't go back again.  Instead,
14567
   we must create a new area with the same attributes and add the new output
14568
   to that.  Unfortunately, there is nothing we can do here to guarantee that
14569
   two areas with the same attributes will be linked adjacently in the
14570
   resulting executable, so we have to be careful not to do pc-relative
14571
   addressing across such boundaries.  */
14572
 
14573
static void
14574
aof_asm_init_sections (void)
14575
{
14576
  text_section = get_unnamed_section (SECTION_CODE,
14577
                                      aof_output_text_section_asm_op, NULL);
14578
  data_section = get_unnamed_section (SECTION_WRITE,
14579
                                      aof_output_data_section_asm_op, NULL);
14580
  readonly_data_section = text_section;
14581
}
14582
 
14583
void
14584
zero_init_section (void)
14585
{
14586
  static int zero_init_count = 1;
14587
 
14588
  fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
14589
  in_section = NULL;
14590
}
14591
 
14592
/* The AOF assembler is religiously strict about declarations of
14593
   imported and exported symbols, so that it is impossible to declare
14594
   a function as imported near the beginning of the file, and then to
14595
   export it later on.  It is, however, possible to delay the decision
14596
   until all the functions in the file have been compiled.  To get
14597
   around this, we maintain a list of the imports and exports, and
14598
   delete from it any that are subsequently defined.  At the end of
14599
   compilation we spit the remainder of the list out before the END
14600
   directive.  */
14601
 
14602
struct import
14603
{
14604
  struct import * next;
14605
  const char * name;
14606
};
14607
 
14608
static struct import * imports_list = NULL;
14609
 
14610
void
14611
aof_add_import (const char *name)
14612
{
14613
  struct import * new;
14614
 
14615
  for (new = imports_list; new; new = new->next)
14616
    if (new->name == name)
14617
      return;
14618
 
14619
  new = (struct import *) xmalloc (sizeof (struct import));
14620
  new->next = imports_list;
14621
  imports_list = new;
14622
  new->name = name;
14623
}
14624
 
14625
void
14626
aof_delete_import (const char *name)
14627
{
14628
  struct import ** old;
14629
 
14630
  for (old = &imports_list; *old; old = & (*old)->next)
14631
    {
14632
      if ((*old)->name == name)
14633
        {
14634
          *old = (*old)->next;
14635
          return;
14636
        }
14637
    }
14638
}
14639
 
14640
int arm_main_function = 0;
14641
 
14642
static void
14643
aof_dump_imports (FILE *f)
14644
{
14645
  /* The AOF assembler needs this to cause the startup code to be extracted
14646
     from the library.  Brining in __main causes the whole thing to work
14647
     automagically.  */
14648
  if (arm_main_function)
14649
    {
14650
      switch_to_section (text_section);
14651
      fputs ("\tIMPORT __main\n", f);
14652
      fputs ("\tDCD __main\n", f);
14653
    }
14654
 
14655
  /* Now dump the remaining imports.  */
14656
  while (imports_list)
14657
    {
14658
      fprintf (f, "\tIMPORT\t");
14659
      assemble_name (f, imports_list->name);
14660
      fputc ('\n', f);
14661
      imports_list = imports_list->next;
14662
    }
14663
}
14664
 
14665
static void
14666
aof_globalize_label (FILE *stream, const char *name)
14667
{
14668
  default_globalize_label (stream, name);
14669
  if (! strcmp (name, "main"))
14670
    arm_main_function = 1;
14671
}
14672
 
14673
static void
14674
aof_file_start (void)
14675
{
14676
  fputs ("__r0\tRN\t0\n", asm_out_file);
14677
  fputs ("__a1\tRN\t0\n", asm_out_file);
14678
  fputs ("__a2\tRN\t1\n", asm_out_file);
14679
  fputs ("__a3\tRN\t2\n", asm_out_file);
14680
  fputs ("__a4\tRN\t3\n", asm_out_file);
14681
  fputs ("__v1\tRN\t4\n", asm_out_file);
14682
  fputs ("__v2\tRN\t5\n", asm_out_file);
14683
  fputs ("__v3\tRN\t6\n", asm_out_file);
14684
  fputs ("__v4\tRN\t7\n", asm_out_file);
14685
  fputs ("__v5\tRN\t8\n", asm_out_file);
14686
  fputs ("__v6\tRN\t9\n", asm_out_file);
14687
  fputs ("__sl\tRN\t10\n", asm_out_file);
14688
  fputs ("__fp\tRN\t11\n", asm_out_file);
14689
  fputs ("__ip\tRN\t12\n", asm_out_file);
14690
  fputs ("__sp\tRN\t13\n", asm_out_file);
14691
  fputs ("__lr\tRN\t14\n", asm_out_file);
14692
  fputs ("__pc\tRN\t15\n", asm_out_file);
14693
  fputs ("__f0\tFN\t0\n", asm_out_file);
14694
  fputs ("__f1\tFN\t1\n", asm_out_file);
14695
  fputs ("__f2\tFN\t2\n", asm_out_file);
14696
  fputs ("__f3\tFN\t3\n", asm_out_file);
14697
  fputs ("__f4\tFN\t4\n", asm_out_file);
14698
  fputs ("__f5\tFN\t5\n", asm_out_file);
14699
  fputs ("__f6\tFN\t6\n", asm_out_file);
14700
  fputs ("__f7\tFN\t7\n", asm_out_file);
14701
  switch_to_section (text_section);
14702
}
14703
 
14704
static void
14705
aof_file_end (void)
14706
{
14707
  if (flag_pic)
14708
    aof_dump_pic_table (asm_out_file);
14709
  arm_file_end ();
14710
  aof_dump_imports (asm_out_file);
14711
  fputs ("\tEND\n", asm_out_file);
14712
}
14713
#endif /* AOF_ASSEMBLER */
14714
 
14715
#ifndef ARM_PE
14716
/* Symbols in the text segment can be accessed without indirecting via the
14717
   constant pool; it may take an extra binary operation, but this is still
14718
   faster than indirecting via memory.  Don't do this when not optimizing,
14719
   since we won't be calculating al of the offsets necessary to do this
14720
   simplification.  */
14721
 
14722
static void
14723
arm_encode_section_info (tree decl, rtx rtl, int first)
14724
{
14725
  /* This doesn't work with AOF syntax, since the string table may be in
14726
     a different AREA.  */
14727
#ifndef AOF_ASSEMBLER
14728
  if (optimize > 0 && TREE_CONSTANT (decl))
14729
    SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14730
#endif
14731
 
14732
  /* If we are referencing a function that is weak then encode a long call
14733
     flag in the function name, otherwise if the function is static or
14734
     or known to be defined in this file then encode a short call flag.  */
14735
  if (first && DECL_P (decl))
14736
    {
14737
      if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14738
        arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14739
      else if (! TREE_PUBLIC (decl))
14740
        arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14741
    }
14742
 
14743
  default_encode_section_info (decl, rtl, first);
14744
}
14745
#endif /* !ARM_PE */
14746
 
14747
static void
14748
arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14749
{
14750
  if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14751
      && !strcmp (prefix, "L"))
14752
    {
14753
      arm_ccfsm_state = 0;
14754
      arm_target_insn = NULL;
14755
    }
14756
  default_internal_label (stream, prefix, labelno);
14757
}
14758
 
14759
/* Output code to add DELTA to the first argument, and then jump
14760
   to FUNCTION.  Used for C++ multiple inheritance.  */
14761
static void
14762
arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14763
                     HOST_WIDE_INT delta,
14764
                     HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14765
                     tree function)
14766
{
14767
  static int thunk_label = 0;
14768
  char label[256];
14769
  char labelpc[256];
14770
  int mi_delta = delta;
14771
  const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14772
  int shift = 0;
14773
  int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14774
                    ? 1 : 0);
14775
  if (mi_delta < 0)
14776
    mi_delta = - mi_delta;
14777
  if (TARGET_THUMB)
14778
    {
14779
      int labelno = thunk_label++;
14780
      ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14781
      fputs ("\tldr\tr12, ", file);
14782
      assemble_name (file, label);
14783
      fputc ('\n', file);
14784
      if (flag_pic)
14785
        {
14786
          /* If we are generating PIC, the ldr instruction below loads
14787
             "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
14788
             the address of the add + 8, so we have:
14789
 
14790
             r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
14791
                 = target + 1.
14792
 
14793
             Note that we have "+ 1" because some versions of GNU ld
14794
             don't set the low bit of the result for R_ARM_REL32
14795
             relocations against thumb function symbols.  */
14796
          ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
14797
          assemble_name (file, labelpc);
14798
          fputs (":\n", file);
14799
          fputs ("\tadd\tr12, pc, r12\n", file);
14800
        }
14801
    }
14802
  while (mi_delta != 0)
14803
    {
14804
      if ((mi_delta & (3 << shift)) == 0)
14805
        shift += 2;
14806
      else
14807
        {
14808
          asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14809
                       mi_op, this_regno, this_regno,
14810
                       mi_delta & (0xff << shift));
14811
          mi_delta &= ~(0xff << shift);
14812
          shift += 8;
14813
        }
14814
    }
14815
  if (TARGET_THUMB)
14816
    {
14817
      fprintf (file, "\tbx\tr12\n");
14818
      ASM_OUTPUT_ALIGN (file, 2);
14819
      assemble_name (file, label);
14820
      fputs (":\n", file);
14821
      if (flag_pic)
14822
        {
14823
          /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
14824
          rtx tem = XEXP (DECL_RTL (function), 0);
14825
          tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
14826
          tem = gen_rtx_MINUS (GET_MODE (tem),
14827
                               tem,
14828
                               gen_rtx_SYMBOL_REF (Pmode,
14829
                                                   ggc_strdup (labelpc)));
14830
          assemble_integer (tem, 4, BITS_PER_WORD, 1);
14831
        }
14832
      else
14833
        /* Output ".word .LTHUNKn".  */
14834
        assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14835
    }
14836
  else
14837
    {
14838
      fputs ("\tb\t", file);
14839
      assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14840
      if (NEED_PLT_RELOC)
14841
        fputs ("(PLT)", file);
14842
      fputc ('\n', file);
14843
    }
14844
}
14845
 
14846
int
14847
arm_emit_vector_const (FILE *file, rtx x)
14848
{
14849
  int i;
14850
  const char * pattern;
14851
 
14852
  gcc_assert (GET_CODE (x) == CONST_VECTOR);
14853
 
14854
  switch (GET_MODE (x))
14855
    {
14856
    case V2SImode: pattern = "%08x"; break;
14857
    case V4HImode: pattern = "%04x"; break;
14858
    case V8QImode: pattern = "%02x"; break;
14859
    default:       gcc_unreachable ();
14860
    }
14861
 
14862
  fprintf (file, "0x");
14863
  for (i = CONST_VECTOR_NUNITS (x); i--;)
14864
    {
14865
      rtx element;
14866
 
14867
      element = CONST_VECTOR_ELT (x, i);
14868
      fprintf (file, pattern, INTVAL (element));
14869
    }
14870
 
14871
  return 1;
14872
}
14873
 
14874
const char *
14875
arm_output_load_gr (rtx *operands)
14876
{
14877
  rtx reg;
14878
  rtx offset;
14879
  rtx wcgr;
14880
  rtx sum;
14881
 
14882
  if (GET_CODE (operands [1]) != MEM
14883
      || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14884
      || GET_CODE (reg = XEXP (sum, 0)) != REG
14885
      || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14886
      || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14887
    return "wldrw%?\t%0, %1";
14888
 
14889
  /* Fix up an out-of-range load of a GR register.  */
14890
  output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14891
  wcgr = operands[0];
14892
  operands[0] = reg;
14893
  output_asm_insn ("ldr%?\t%0, %1", operands);
14894
 
14895
  operands[0] = wcgr;
14896
  operands[1] = reg;
14897
  output_asm_insn ("tmcr%?\t%0, %1", operands);
14898
  output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14899
 
14900
  return "";
14901
}
14902
 
14903
/* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14904
 
14905
   On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14906
   named arg and all anonymous args onto the stack.
14907
   XXX I know the prologue shouldn't be pushing registers, but it is faster
14908
   that way.  */
14909
 
14910
static void
14911
arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14912
                            enum machine_mode mode ATTRIBUTE_UNUSED,
14913
                            tree type ATTRIBUTE_UNUSED,
14914
                            int *pretend_size,
14915
                            int second_time ATTRIBUTE_UNUSED)
14916
{
14917
  cfun->machine->uses_anonymous_args = 1;
14918
  if (cum->nregs < NUM_ARG_REGS)
14919
    *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14920
}
14921
 
14922
/* Return nonzero if the CONSUMER instruction (a store) does not need
14923
   PRODUCER's value to calculate the address.  */
14924
 
14925
int
14926
arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14927
{
14928
  rtx value = PATTERN (producer);
14929
  rtx addr = PATTERN (consumer);
14930
 
14931
  if (GET_CODE (value) == COND_EXEC)
14932
    value = COND_EXEC_CODE (value);
14933
  if (GET_CODE (value) == PARALLEL)
14934
    value = XVECEXP (value, 0, 0);
14935
  value = XEXP (value, 0);
14936
  if (GET_CODE (addr) == COND_EXEC)
14937
    addr = COND_EXEC_CODE (addr);
14938
  if (GET_CODE (addr) == PARALLEL)
14939
    addr = XVECEXP (addr, 0, 0);
14940
  addr = XEXP (addr, 0);
14941
 
14942
  return !reg_overlap_mentioned_p (value, addr);
14943
}
14944
 
14945
/* Return nonzero if the CONSUMER instruction (an ALU op) does not
14946
   have an early register shift value or amount dependency on the
14947
   result of PRODUCER.  */
14948
 
14949
int
14950
arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14951
{
14952
  rtx value = PATTERN (producer);
14953
  rtx op = PATTERN (consumer);
14954
  rtx early_op;
14955
 
14956
  if (GET_CODE (value) == COND_EXEC)
14957
    value = COND_EXEC_CODE (value);
14958
  if (GET_CODE (value) == PARALLEL)
14959
    value = XVECEXP (value, 0, 0);
14960
  value = XEXP (value, 0);
14961
  if (GET_CODE (op) == COND_EXEC)
14962
    op = COND_EXEC_CODE (op);
14963
  if (GET_CODE (op) == PARALLEL)
14964
    op = XVECEXP (op, 0, 0);
14965
  op = XEXP (op, 1);
14966
 
14967
  early_op = XEXP (op, 0);
14968
  /* This is either an actual independent shift, or a shift applied to
14969
     the first operand of another operation.  We want the whole shift
14970
     operation.  */
14971
  if (GET_CODE (early_op) == REG)
14972
    early_op = op;
14973
 
14974
  return !reg_overlap_mentioned_p (value, early_op);
14975
}
14976
 
14977
/* Return nonzero if the CONSUMER instruction (an ALU op) does not
14978
   have an early register shift value dependency on the result of
14979
   PRODUCER.  */
14980
 
14981
int
14982
arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14983
{
14984
  rtx value = PATTERN (producer);
14985
  rtx op = PATTERN (consumer);
14986
  rtx early_op;
14987
 
14988
  if (GET_CODE (value) == COND_EXEC)
14989
    value = COND_EXEC_CODE (value);
14990
  if (GET_CODE (value) == PARALLEL)
14991
    value = XVECEXP (value, 0, 0);
14992
  value = XEXP (value, 0);
14993
  if (GET_CODE (op) == COND_EXEC)
14994
    op = COND_EXEC_CODE (op);
14995
  if (GET_CODE (op) == PARALLEL)
14996
    op = XVECEXP (op, 0, 0);
14997
  op = XEXP (op, 1);
14998
 
14999
  early_op = XEXP (op, 0);
15000
 
15001
  /* This is either an actual independent shift, or a shift applied to
15002
     the first operand of another operation.  We want the value being
15003
     shifted, in either case.  */
15004
  if (GET_CODE (early_op) != REG)
15005
    early_op = XEXP (early_op, 0);
15006
 
15007
  return !reg_overlap_mentioned_p (value, early_op);
15008
}
15009
 
15010
/* Return nonzero if the CONSUMER (a mul or mac op) does not
15011
   have an early register mult dependency on the result of
15012
   PRODUCER.  */
15013
 
15014
int
15015
arm_no_early_mul_dep (rtx producer, rtx consumer)
15016
{
15017
  rtx value = PATTERN (producer);
15018
  rtx op = PATTERN (consumer);
15019
 
15020
  if (GET_CODE (value) == COND_EXEC)
15021
    value = COND_EXEC_CODE (value);
15022
  if (GET_CODE (value) == PARALLEL)
15023
    value = XVECEXP (value, 0, 0);
15024
  value = XEXP (value, 0);
15025
  if (GET_CODE (op) == COND_EXEC)
15026
    op = COND_EXEC_CODE (op);
15027
  if (GET_CODE (op) == PARALLEL)
15028
    op = XVECEXP (op, 0, 0);
15029
  op = XEXP (op, 1);
15030
 
15031
  return (GET_CODE (op) == PLUS
15032
          && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
15033
}
15034
 
15035
 
15036
/* We can't rely on the caller doing the proper promotion when
15037
   using APCS or ATPCS.  */
15038
 
15039
static bool
15040
arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
15041
{
15042
    return !TARGET_AAPCS_BASED;
15043
}
15044
 
15045
 
15046
/* AAPCS based ABIs use short enums by default.  */
15047
 
15048
static bool
15049
arm_default_short_enums (void)
15050
{
15051
  return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
15052
}
15053
 
15054
 
15055
/* AAPCS requires that anonymous bitfields affect structure alignment.  */
15056
 
15057
static bool
15058
arm_align_anon_bitfield (void)
15059
{
15060
  return TARGET_AAPCS_BASED;
15061
}
15062
 
15063
 
15064
/* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
15065
 
15066
static tree
15067
arm_cxx_guard_type (void)
15068
{
15069
  return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
15070
}
15071
 
15072
 
15073
/* The EABI says test the least significant bit of a guard variable.  */
15074
 
15075
static bool
15076
arm_cxx_guard_mask_bit (void)
15077
{
15078
  return TARGET_AAPCS_BASED;
15079
}
15080
 
15081
 
15082
/* The EABI specifies that all array cookies are 8 bytes long.  */
15083
 
15084
static tree
15085
arm_get_cookie_size (tree type)
15086
{
15087
  tree size;
15088
 
15089
  if (!TARGET_AAPCS_BASED)
15090
    return default_cxx_get_cookie_size (type);
15091
 
15092
  size = build_int_cst (sizetype, 8);
15093
  return size;
15094
}
15095
 
15096
 
15097
/* The EABI says that array cookies should also contain the element size.  */
15098
 
15099
static bool
15100
arm_cookie_has_size (void)
15101
{
15102
  return TARGET_AAPCS_BASED;
15103
}
15104
 
15105
 
15106
/* The EABI says constructors and destructors should return a pointer to
15107
   the object constructed/destroyed.  */
15108
 
15109
static bool
15110
arm_cxx_cdtor_returns_this (void)
15111
{
15112
  return TARGET_AAPCS_BASED;
15113
}
15114
 
15115
/* The EABI says that an inline function may never be the key
15116
   method.  */
15117
 
15118
static bool
15119
arm_cxx_key_method_may_be_inline (void)
15120
{
15121
  return !TARGET_AAPCS_BASED;
15122
}
15123
 
15124
static void
15125
arm_cxx_determine_class_data_visibility (tree decl)
15126
{
15127
  if (!TARGET_AAPCS_BASED)
15128
    return;
15129
 
15130
  /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
15131
     is exported.  However, on systems without dynamic vague linkage,
15132
     \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
15133
  if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
15134
    DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
15135
  else
15136
    DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
15137
  DECL_VISIBILITY_SPECIFIED (decl) = 1;
15138
}
15139
 
15140
static bool
15141
arm_cxx_class_data_always_comdat (void)
15142
{
15143
  /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
15144
     vague linkage if the class has no key function.  */
15145
  return !TARGET_AAPCS_BASED;
15146
}
15147
 
15148
 
15149
/* The EABI says __aeabi_atexit should be used to register static
15150
   destructors.  */
15151
 
15152
static bool
15153
arm_cxx_use_aeabi_atexit (void)
15154
{
15155
  return TARGET_AAPCS_BASED;
15156
}
15157
 
15158
 
15159
void
15160
arm_set_return_address (rtx source, rtx scratch)
15161
{
15162
  arm_stack_offsets *offsets;
15163
  HOST_WIDE_INT delta;
15164
  rtx addr;
15165
  unsigned long saved_regs;
15166
 
15167
  saved_regs = arm_compute_save_reg_mask ();
15168
 
15169
  if ((saved_regs & (1 << LR_REGNUM)) == 0)
15170
    emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15171
  else
15172
    {
15173
      if (frame_pointer_needed)
15174
        addr = plus_constant(hard_frame_pointer_rtx, -4);
15175
      else
15176
        {
15177
          /* LR will be the first saved register.  */
15178
          offsets = arm_get_frame_offsets ();
15179
          delta = offsets->outgoing_args - (offsets->frame + 4);
15180
 
15181
 
15182
          if (delta >= 4096)
15183
            {
15184
              emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
15185
                                     GEN_INT (delta & ~4095)));
15186
              addr = scratch;
15187
              delta &= 4095;
15188
            }
15189
          else
15190
            addr = stack_pointer_rtx;
15191
 
15192
          addr = plus_constant (addr, delta);
15193
        }
15194
      emit_move_insn (gen_frame_mem (Pmode, addr), source);
15195
    }
15196
}
15197
 
15198
 
15199
void
15200
thumb_set_return_address (rtx source, rtx scratch)
15201
{
15202
  arm_stack_offsets *offsets;
15203
  HOST_WIDE_INT delta;
15204
  int reg;
15205
  rtx addr;
15206
  unsigned long mask;
15207
 
15208
  emit_insn (gen_rtx_USE (VOIDmode, source));
15209
 
15210
  mask = thumb_compute_save_reg_mask ();
15211
  if (mask & (1 << LR_REGNUM))
15212
    {
15213
      offsets = arm_get_frame_offsets ();
15214
 
15215
      /* Find the saved regs.  */
15216
      if (frame_pointer_needed)
15217
        {
15218
          delta = offsets->soft_frame - offsets->saved_args;
15219
          reg = THUMB_HARD_FRAME_POINTER_REGNUM;
15220
        }
15221
      else
15222
        {
15223
          delta = offsets->outgoing_args - offsets->saved_args;
15224
          reg = SP_REGNUM;
15225
        }
15226
      /* Allow for the stack frame.  */
15227
      if (TARGET_BACKTRACE)
15228
        delta -= 16;
15229
      /* The link register is always the first saved register.  */
15230
      delta -= 4;
15231
 
15232
      /* Construct the address.  */
15233
      addr = gen_rtx_REG (SImode, reg);
15234
      if ((reg != SP_REGNUM && delta >= 128)
15235
          || delta >= 1024)
15236
        {
15237
          emit_insn (gen_movsi (scratch, GEN_INT (delta)));
15238
          emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
15239
          addr = scratch;
15240
        }
15241
      else
15242
        addr = plus_constant (addr, delta);
15243
 
15244
      emit_move_insn (gen_frame_mem (Pmode, addr), source);
15245
    }
15246
  else
15247
    emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15248
}
15249
 
15250
/* Implements target hook vector_mode_supported_p.  */
15251
bool
15252
arm_vector_mode_supported_p (enum machine_mode mode)
15253
{
15254
  if ((mode == V2SImode)
15255
      || (mode == V4HImode)
15256
      || (mode == V8QImode))
15257
    return true;
15258
 
15259
  return false;
15260
}
15261
 
15262
/* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
15263
   ARM insns and therefore guarantee that the shift count is modulo 256.
15264
   DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
15265
   guarantee no particular behavior for out-of-range counts.  */
15266
 
15267
static unsigned HOST_WIDE_INT
15268
arm_shift_truncation_mask (enum machine_mode mode)
15269
{
15270
  return mode == SImode ? 255 : 0;
15271
}
15272
 
15273
 
15274
/* Map internal gcc register numbers to DWARF2 register numbers.  */
15275
 
15276
unsigned int
15277
arm_dbx_register_number (unsigned int regno)
15278
{
15279
  if (regno < 16)
15280
    return regno;
15281
 
15282
  /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
15283
     compatibility.  The EABI defines them as registers 96-103.  */
15284
  if (IS_FPA_REGNUM (regno))
15285
    return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
15286
 
15287
  if (IS_VFP_REGNUM (regno))
15288
    return 64 + regno - FIRST_VFP_REGNUM;
15289
 
15290
  if (IS_IWMMXT_GR_REGNUM (regno))
15291
    return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
15292
 
15293
  if (IS_IWMMXT_REGNUM (regno))
15294
    return 112 + regno - FIRST_IWMMXT_REGNUM;
15295
 
15296
  gcc_unreachable ();
15297
}
15298
 
15299
 
15300
#ifdef TARGET_UNWIND_INFO
15301
/* Emit unwind directives for a store-multiple instruction.  This should
15302
   only ever be generated by the function prologue code, so we expect it
15303
   to have a particular form.  */
15304
 
15305
static void
15306
arm_unwind_emit_stm (FILE * asm_out_file, rtx p)
15307
{
15308
  int i;
15309
  HOST_WIDE_INT offset;
15310
  HOST_WIDE_INT nregs;
15311
  int reg_size;
15312
  unsigned reg;
15313
  unsigned lastreg;
15314
  rtx e;
15315
 
15316
  /* First insn will adjust the stack pointer.  */
15317
  e = XVECEXP (p, 0, 0);
15318
  if (GET_CODE (e) != SET
15319
      || GET_CODE (XEXP (e, 0)) != REG
15320
      || REGNO (XEXP (e, 0)) != SP_REGNUM
15321
      || GET_CODE (XEXP (e, 1)) != PLUS)
15322
    abort ();
15323
 
15324
  offset = -INTVAL (XEXP (XEXP (e, 1), 1));
15325
  nregs = XVECLEN (p, 0) - 1;
15326
 
15327
  reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
15328
  if (reg < 16)
15329
    {
15330
      /* The function prologue may also push pc, but not annotate it as it is
15331
         never restored.  We turn this into a stack pointer adjustment.  */
15332
      if (nregs * 4 == offset - 4)
15333
        {
15334
          fprintf (asm_out_file, "\t.pad #4\n");
15335
          offset -= 4;
15336
        }
15337
      reg_size = 4;
15338
    }
15339
  else if (IS_VFP_REGNUM (reg))
15340
    {
15341
      /* FPA register saves use an additional word.  */
15342
      offset -= 4;
15343
      reg_size = 8;
15344
    }
15345
  else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
15346
    {
15347
      /* FPA registers are done differently.  */
15348
      asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
15349
      return;
15350
    }
15351
  else
15352
    /* Unknown register type.  */
15353
    abort ();
15354
 
15355
  /* If the stack increment doesn't match the size of the saved registers,
15356
     something has gone horribly wrong.  */
15357
  if (offset != nregs * reg_size)
15358
    abort ();
15359
 
15360
  fprintf (asm_out_file, "\t.save {");
15361
 
15362
  offset = 0;
15363
  lastreg = 0;
15364
  /* The remaining insns will describe the stores.  */
15365
  for (i = 1; i <= nregs; i++)
15366
    {
15367
      /* Expect (set (mem <addr>) (reg)).
15368
         Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
15369
      e = XVECEXP (p, 0, i);
15370
      if (GET_CODE (e) != SET
15371
          || GET_CODE (XEXP (e, 0)) != MEM
15372
          || GET_CODE (XEXP (e, 1)) != REG)
15373
        abort ();
15374
 
15375
      reg = REGNO (XEXP (e, 1));
15376
      if (reg < lastreg)
15377
        abort ();
15378
 
15379
      if (i != 1)
15380
        fprintf (asm_out_file, ", ");
15381
      /* We can't use %r for vfp because we need to use the
15382
         double precision register names.  */
15383
      if (IS_VFP_REGNUM (reg))
15384
        asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
15385
      else
15386
        asm_fprintf (asm_out_file, "%r", reg);
15387
 
15388
#ifdef ENABLE_CHECKING
15389
      /* Check that the addresses are consecutive.  */
15390
      e = XEXP (XEXP (e, 0), 0);
15391
      if (GET_CODE (e) == PLUS)
15392
        {
15393
          offset += reg_size;
15394
          if (GET_CODE (XEXP (e, 0)) != REG
15395
              || REGNO (XEXP (e, 0)) != SP_REGNUM
15396
              || GET_CODE (XEXP (e, 1)) != CONST_INT
15397
              || offset != INTVAL (XEXP (e, 1)))
15398
            abort ();
15399
        }
15400
      else if (i != 1
15401
               || GET_CODE (e) != REG
15402
               || REGNO (e) != SP_REGNUM)
15403
        abort ();
15404
#endif
15405
    }
15406
  fprintf (asm_out_file, "}\n");
15407
}
15408
 
15409
/*  Emit unwind directives for a SET.  */
15410
 
15411
static void
15412
arm_unwind_emit_set (FILE * asm_out_file, rtx p)
15413
{
15414
  rtx e0;
15415
  rtx e1;
15416
 
15417
  e0 = XEXP (p, 0);
15418
  e1 = XEXP (p, 1);
15419
  switch (GET_CODE (e0))
15420
    {
15421
    case MEM:
15422
      /* Pushing a single register.  */
15423
      if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
15424
          || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
15425
          || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
15426
        abort ();
15427
 
15428
      asm_fprintf (asm_out_file, "\t.save ");
15429
      if (IS_VFP_REGNUM (REGNO (e1)))
15430
        asm_fprintf(asm_out_file, "{d%d}\n",
15431
                    (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
15432
      else
15433
        asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
15434
      break;
15435
 
15436
    case REG:
15437
      if (REGNO (e0) == SP_REGNUM)
15438
        {
15439
          /* A stack increment.  */
15440
          if (GET_CODE (e1) != PLUS
15441
              || GET_CODE (XEXP (e1, 0)) != REG
15442
              || REGNO (XEXP (e1, 0)) != SP_REGNUM
15443
              || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15444
            abort ();
15445
 
15446
          asm_fprintf (asm_out_file, "\t.pad #%wd\n",
15447
                       -INTVAL (XEXP (e1, 1)));
15448
        }
15449
      else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
15450
        {
15451
          HOST_WIDE_INT offset;
15452
          unsigned reg;
15453
 
15454
          if (GET_CODE (e1) == PLUS)
15455
            {
15456
              if (GET_CODE (XEXP (e1, 0)) != REG
15457
                  || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15458
                abort ();
15459
              reg = REGNO (XEXP (e1, 0));
15460
              offset = INTVAL (XEXP (e1, 1));
15461
              asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
15462
                           HARD_FRAME_POINTER_REGNUM, reg,
15463
                           INTVAL (XEXP (e1, 1)));
15464
            }
15465
          else if (GET_CODE (e1) == REG)
15466
            {
15467
              reg = REGNO (e1);
15468
              asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
15469
                           HARD_FRAME_POINTER_REGNUM, reg);
15470
            }
15471
          else
15472
            abort ();
15473
        }
15474
      else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
15475
        {
15476
          /* Move from sp to reg.  */
15477
          asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
15478
        }
15479
     else if (GET_CODE (e1) == PLUS
15480
              && GET_CODE (XEXP (e1, 0)) == REG
15481
              && REGNO (XEXP (e1, 0)) == SP_REGNUM
15482
              && GET_CODE (XEXP (e1, 1)) == CONST_INT)
15483
        {
15484
          /* Set reg to offset from sp.  */
15485
          asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
15486
                       REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
15487
        }
15488
      else
15489
        abort ();
15490
      break;
15491
 
15492
    default:
15493
      abort ();
15494
    }
15495
}
15496
 
15497
 
15498
/* Emit unwind directives for the given insn.  */
15499
 
15500
static void
15501
arm_unwind_emit (FILE * asm_out_file, rtx insn)
15502
{
15503
  rtx pat;
15504
 
15505
  if (!ARM_EABI_UNWIND_TABLES)
15506
    return;
15507
 
15508
  if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
15509
    return;
15510
 
15511
  pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
15512
  if (pat)
15513
    pat = XEXP (pat, 0);
15514
  else
15515
    pat = PATTERN (insn);
15516
 
15517
  switch (GET_CODE (pat))
15518
    {
15519
    case SET:
15520
      arm_unwind_emit_set (asm_out_file, pat);
15521
      break;
15522
 
15523
    case SEQUENCE:
15524
      /* Store multiple.  */
15525
      arm_unwind_emit_stm (asm_out_file, pat);
15526
      break;
15527
 
15528
    default:
15529
      abort();
15530
    }
15531
}
15532
 
15533
 
15534
/* Output a reference from a function exception table to the type_info
15535
   object X.  The EABI specifies that the symbol should be relocated by
15536
   an R_ARM_TARGET2 relocation.  */
15537
 
15538
static bool
15539
arm_output_ttype (rtx x)
15540
{
15541
  fputs ("\t.word\t", asm_out_file);
15542
  output_addr_const (asm_out_file, x);
15543
  /* Use special relocations for symbol references.  */
15544
  if (GET_CODE (x) != CONST_INT)
15545
    fputs ("(TARGET2)", asm_out_file);
15546
  fputc ('\n', asm_out_file);
15547
 
15548
  return TRUE;
15549
}
15550
#endif /* TARGET_UNWIND_INFO */
15551
 
15552
 
15553
/* Output unwind directives for the start/end of a function.  */
15554
 
15555
void
15556
arm_output_fn_unwind (FILE * f, bool prologue)
15557
{
15558
  if (!ARM_EABI_UNWIND_TABLES)
15559
    return;
15560
 
15561
  if (prologue)
15562
    fputs ("\t.fnstart\n", f);
15563
  else
15564
    fputs ("\t.fnend\n", f);
15565
}
15566
 
15567
static bool
15568
arm_emit_tls_decoration (FILE *fp, rtx x)
15569
{
15570
  enum tls_reloc reloc;
15571
  rtx val;
15572
 
15573
  val = XVECEXP (x, 0, 0);
15574
  reloc = INTVAL (XVECEXP (x, 0, 1));
15575
 
15576
  output_addr_const (fp, val);
15577
 
15578
  switch (reloc)
15579
    {
15580
    case TLS_GD32:
15581
      fputs ("(tlsgd)", fp);
15582
      break;
15583
    case TLS_LDM32:
15584
      fputs ("(tlsldm)", fp);
15585
      break;
15586
    case TLS_LDO32:
15587
      fputs ("(tlsldo)", fp);
15588
      break;
15589
    case TLS_IE32:
15590
      fputs ("(gottpoff)", fp);
15591
      break;
15592
    case TLS_LE32:
15593
      fputs ("(tpoff)", fp);
15594
      break;
15595
    default:
15596
      gcc_unreachable ();
15597
    }
15598
 
15599
  switch (reloc)
15600
    {
15601
    case TLS_GD32:
15602
    case TLS_LDM32:
15603
    case TLS_IE32:
15604
      fputs (" + (. - ", fp);
15605
      output_addr_const (fp, XVECEXP (x, 0, 2));
15606
      fputs (" - ", fp);
15607
      output_addr_const (fp, XVECEXP (x, 0, 3));
15608
      fputc (')', fp);
15609
      break;
15610
    default:
15611
      break;
15612
    }
15613
 
15614
  return TRUE;
15615
}
15616
 
15617
bool
15618
arm_output_addr_const_extra (FILE *fp, rtx x)
15619
{
15620
  if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
15621
    return arm_emit_tls_decoration (fp, x);
15622
  else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
15623
    {
15624
      char label[256];
15625
      int labelno = INTVAL (XVECEXP (x, 0, 0));
15626
 
15627
      ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
15628
      assemble_name_raw (fp, label);
15629
 
15630
      return TRUE;
15631
    }
15632
  else if (GET_CODE (x) == CONST_VECTOR)
15633
    return arm_emit_vector_const (fp, x);
15634
 
15635
  return FALSE;
15636
}
15637
 
15638
#include "gt-arm.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.