]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/darwin.c
* doc/rtl.texi (SYMBOL_REF_CONSTANT, SYMBOL_REF_DATA): Document.
[thirdparty/gcc.git] / gcc / config / darwin.c
CommitLineData
80d725d7 1/* Functions for generic Darwin as target machine for GNU C compiler.
2be1b07e 2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003, 2004,
3 2005
80d725d7 4 Free Software Foundation, Inc.
5 Contributed by Apple Computer Inc.
6
187b36cf 7This file is part of GCC.
80d725d7 8
187b36cf 9GCC is free software; you can redistribute it and/or modify
80d725d7 10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
187b36cf 14GCC is distributed in the hope that it will be useful,
80d725d7 15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
187b36cf 20along with GCC; see the file COPYING. If not, write to
dbddc6c4 21the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22Boston, MA 02110-1301, USA. */
80d725d7 23
24#include "config.h"
25#include "system.h"
805e22b2 26#include "coretypes.h"
27#include "tm.h"
80d725d7 28#include "rtl.h"
29#include "regs.h"
30#include "hard-reg-set.h"
31#include "real.h"
32#include "insn-config.h"
33#include "conditions.h"
34#include "insn-flags.h"
35#include "output.h"
36#include "insn-attr.h"
37#include "flags.h"
38#include "tree.h"
39#include "expr.h"
40#include "reload.h"
80d725d7 41#include "function.h"
42#include "ggc.h"
d19bd1f0 43#include "langhooks.h"
6d3abfc0 44#include "target.h"
79e2a0ca 45#include "tm_p.h"
690abe5d 46#include "toplev.h"
ab06d2ff 47#include "hashtab.h"
80d725d7 48
f0ed0e8a 49/* Darwin supports a feature called fix-and-continue, which is used
50 for rapid turn around debugging. When code is compiled with the
51 -mfix-and-continue flag, two changes are made to the generated code
52 that allow the system to do things that it would normally not be
53 able to do easily. These changes allow gdb to load in
54 recompilation of a translation unit that has been changed into a
55 running program and replace existing functions and methods of that
cb4213cd 56 translation unit with versions of those functions and methods
f0ed0e8a 57 from the newly compiled translation unit. The new functions access
d188633a 58 the existing static symbols from the old translation unit, if the
59 symbol existed in the unit to be replaced, and from the new
60 translation unit, otherwise.
f0ed0e8a 61
8063e558 62 The changes are to insert 5 nops at the beginning of all functions
d188633a 63 and to use indirection to get at static symbols. The 5 nops
f0ed0e8a 64 are required by consumers of the generated code. Currently, gdb
65 uses this to patch in a jump to the overriding function, this
66 allows all uses of the old name to forward to the replacement,
84cbcde5 67 including existing function pointers and virtual methods. See
f0ed0e8a 68 rs6000_emit_prologue for the code that handles the nop insertions.
b215c058 69
f0ed0e8a 70 The added indirection allows gdb to redirect accesses to static
d188633a 71 symbols from the newly loaded translation unit to the existing
72 symbol, if any. @code{static} symbols are special and are handled by
73 setting the second word in the .non_lazy_symbol_pointer data
74 structure to symbol. See indirect_data for the code that handles
75 the extra indirection, and machopic_output_indirection and its use
76 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
77 symbol indirection. */
f0ed0e8a 78
92e4ba7d 79/* Section names. */
80section * darwin_sections[NUM_DARWIN_SECTIONS];
2f14b1f9 81
82/* A get_unnamed_section callback used to switch to an ObjC section.
83 DIRECTIVE is as for output_section_asm_op. */
84
85static void
86output_objc_section_asm_op (const void *directive)
87{
92e4ba7d 88 static bool been_here = false;
2f14b1f9 89
92e4ba7d 90 if (! been_here)
2f14b1f9 91 {
b215c058 92 static const enum darwin_section_enum tomark[] =
92e4ba7d 93 {
94 /* written, cold -> hot */
95 objc_cat_cls_meth_section,
96 objc_cat_inst_meth_section,
97 objc_string_object_section,
98 objc_constant_string_object_section,
99 objc_selector_refs_section,
100 objc_selector_fixup_section,
101 objc_cls_refs_section,
102 objc_class_section,
103 objc_meta_class_section,
104 /* shared, hot -> cold */
105 objc_cls_meth_section,
106 objc_inst_meth_section,
107 objc_protocol_section,
108 objc_class_names_section,
109 objc_meth_var_types_section,
110 objc_meth_var_names_section,
111 objc_category_section,
112 objc_class_vars_section,
113 objc_instance_vars_section,
114 objc_module_info_section,
115 objc_symbols_section
116 };
117 size_t i;
b215c058 118
92e4ba7d 119 been_here = true;
120 for (i = 0; i < ARRAY_SIZE (tomark); i++)
121 switch_to_section (darwin_sections[tomark[i]]);
2f14b1f9 122 }
123 output_section_asm_op (directive);
124}
125
126/* Implement TARGET_ASM_INIT_SECTIONS. */
127
128void
129darwin_init_sections (void)
130{
92e4ba7d 131#define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
132 darwin_sections[NAME] = \
133 get_unnamed_section (FLAGS, (OBJC \
134 ? output_objc_section_asm_op \
135 : output_section_asm_op), \
136 "\t" DIRECTIVE);
137#include "config/darwin-sections.def"
2f14b1f9 138#undef DEF_SECTION
139
92e4ba7d 140 readonly_data_section = darwin_sections[const_section];
141 exception_section = darwin_sections[darwin_exception_section];
142 eh_frame_section = darwin_sections[darwin_eh_frame_section];
2f14b1f9 143}
f0ed0e8a 144
80d725d7 145int
b40da9a7 146name_needs_quotes (const char *name)
80d725d7 147{
148 int c;
149 while ((c = *name++) != '\0')
37ec5024 150 if (! ISIDNUM (c) && c != '.' && c != '$')
80d725d7 151 return 1;
152 return 0;
153}
154
866318b4 155/* Return true if SYM_REF can be used without an indirection. */
ab06d2ff 156static int
157machopic_symbol_defined_p (rtx sym_ref)
80d725d7 158{
866318b4 159 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
160 return true;
161
162 /* If a symbol references local and is not an extern to this
163 file, then the symbol might be able to declared as defined. */
164 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
165 {
166 /* If the symbol references a variable and the variable is a
167 common symbol, then this symbol is not defined. */
168 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
169 {
170 tree decl = SYMBOL_REF_DECL (sym_ref);
171 if (!decl)
172 return true;
173 if (DECL_COMMON (decl))
174 return false;
175 }
176 return true;
177 }
178 return false;
80d725d7 179}
180
ab06d2ff 181/* This module assumes that (const (symbol_ref "foo")) is a legal pic
182 reference, which will not be changed. */
b40da9a7 183
80d725d7 184enum machopic_addr_class
ab06d2ff 185machopic_classify_symbol (rtx sym_ref)
80d725d7 186{
ab06d2ff 187 int flags;
188 bool function_p;
189
190 flags = SYMBOL_REF_FLAGS (sym_ref);
191 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
192 if (machopic_symbol_defined_p (sym_ref))
b215c058 193 return (function_p
ab06d2ff 194 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
195 else
b215c058 196 return (function_p
ab06d2ff 197 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
80d725d7 198}
199
f0ed0e8a 200#ifndef TARGET_FIX_AND_CONTINUE
201#define TARGET_FIX_AND_CONTINUE 0
202#endif
203
204/* Indicate when fix-and-continue style code generation is being used
205 and when a reference to data should be indirected so that it can be
0975351b 206 rebound in a new translation unit to reference the original instance
f0ed0e8a 207 of that data. Symbol names that are for code generation local to
208 the translation unit are bound to the new translation unit;
209 currently this means symbols that begin with L or _OBJC_;
210 otherwise, we indicate that an indirect reference should be made to
211 permit the runtime to rebind new instances of the translation unit
212 to the original instance of the data. */
213
214static int
215indirect_data (rtx sym_ref)
216{
217 int lprefix;
218 const char *name;
219
220 /* If we aren't generating fix-and-continue code, don't do anything special. */
221 if (TARGET_FIX_AND_CONTINUE == 0)
222 return 0;
223
224 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
225 are indirected. Symbols that begin with L and _OBJC_ are always
226 bound to the current translation unit as they are used for
227 generated local data of the translation unit. */
228
229 name = XSTR (sym_ref, 0);
230
231 lprefix = (((name[0] == '*' || name[0] == '&')
232 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
78a79402 233 || (strncmp (name, "_OBJC_", 6) == 0));
f0ed0e8a 234
235 return ! lprefix;
236}
237
238
80d725d7 239static int
ab06d2ff 240machopic_data_defined_p (rtx sym_ref)
80d725d7 241{
f0ed0e8a 242 if (indirect_data (sym_ref))
243 return 0;
244
ab06d2ff 245 switch (machopic_classify_symbol (sym_ref))
80d725d7 246 {
247 case MACHOPIC_DEFINED_DATA:
3a7ccce8 248 case MACHOPIC_DEFINED_FUNCTION:
80d725d7 249 return 1;
250 default:
251 return 0;
252 }
253}
254
80d725d7 255void
ab06d2ff 256machopic_define_symbol (rtx mem)
80d725d7 257{
ab06d2ff 258 rtx sym_ref;
b215c058 259
27d2baef 260 gcc_assert (GET_CODE (mem) == MEM);
ab06d2ff 261 sym_ref = XEXP (mem, 0);
262 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
80d725d7 263}
264
3a32429b 265static GTY(()) char * function_base;
80d725d7 266
a7260dec 267const char *
b40da9a7 268machopic_function_base_name (void)
80d725d7 269{
8f74ae7b 270 /* if dynamic-no-pic is on, we should not get here */
27d2baef 271 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
80d725d7 272
b186bdc9 273 if (function_base == NULL)
b40da9a7 274 function_base =
b186bdc9 275 (char *) ggc_alloc_string ("<pic base>", sizeof ("<pic base>"));
276
277 current_function_uses_pic_offset_table = 1;
278
279 return function_base;
280}
281
ab06d2ff 282/* Return a SYMBOL_REF for the PIC function base. */
283
284rtx
285machopic_function_base_sym (void)
286{
287 rtx sym_ref;
288
289 sym_ref = gen_rtx_SYMBOL_REF (Pmode, machopic_function_base_name ());
b215c058 290 SYMBOL_REF_FLAGS (sym_ref)
ab06d2ff 291 |= (MACHO_SYMBOL_FLAG_VARIABLE | MACHO_SYMBOL_FLAG_DEFINED);
292 return sym_ref;
293}
294
b06a3cc3 295/* Return either ORIG or (const:P (minus:P ORIG PIC_BASE)), depending
296 on whether pic_base is NULL or not. */
297static inline rtx
298gen_pic_offset (rtx orig, rtx pic_base)
299{
300 if (!pic_base)
301 return orig;
302 else
303 return gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, orig, pic_base));
304}
305
b186bdc9 306static GTY(()) const char * function_base_func_name;
307static GTY(()) int current_pic_label_num;
308
309void
310machopic_output_function_base_name (FILE *file)
311{
312 const char *current_name;
313
1d60d981 314 /* If dynamic-no-pic is on, we should not get here. */
27d2baef 315 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
b40da9a7 316 current_name =
b186bdc9 317 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
3a32429b 318 if (function_base_func_name != current_name)
80d725d7 319 {
80d725d7 320 ++current_pic_label_num;
3a32429b 321 function_base_func_name = current_name;
80d725d7 322 }
b186bdc9 323 fprintf (file, "\"L%011d$pb\"", current_pic_label_num);
80d725d7 324}
325
ab06d2ff 326/* The suffix attached to non-lazy pointer symbols. */
327#define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
328/* The suffix attached to stub symbols. */
329#define STUB_SUFFIX "$stub"
80d725d7 330
ab06d2ff 331typedef struct machopic_indirection GTY (())
80d725d7 332{
ab06d2ff 333 /* The SYMBOL_REF for the entity referenced. */
334 rtx symbol;
6d3abfc0 335 /* The name of the stub or non-lazy pointer. */
336 const char * ptr_name;
ab06d2ff 337 /* True iff this entry is for a stub (as opposed to a non-lazy
338 pointer). */
339 bool stub_p;
340 /* True iff this stub or pointer pointer has been referenced. */
341 bool used;
342} machopic_indirection;
343
344/* A table mapping stub names and non-lazy pointer names to
345 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
346
b215c058 347static GTY ((param_is (struct machopic_indirection))) htab_t
ab06d2ff 348 machopic_indirections;
349
350/* Return a hash value for a SLOT in the indirections hash table. */
351
352static hashval_t
353machopic_indirection_hash (const void *slot)
354{
355 const machopic_indirection *p = (const machopic_indirection *) slot;
6d3abfc0 356 return htab_hash_string (p->ptr_name);
ab06d2ff 357}
b40da9a7 358
ab06d2ff 359/* Returns true if the KEY is the same as that associated with
360 SLOT. */
361
362static int
363machopic_indirection_eq (const void *slot, const void *key)
364{
6d3abfc0 365 return strcmp (((const machopic_indirection *) slot)->ptr_name, key) == 0;
ab06d2ff 366}
80d725d7 367
ab06d2ff 368/* Return the name of the non-lazy pointer (if STUB_P is false) or
369 stub (if STUB_B is true) corresponding to the given name. */
8dfaa51b 370
ab06d2ff 371const char *
372machopic_indirection_name (rtx sym_ref, bool stub_p)
373{
374 char *buffer;
375 const char *name = XSTR (sym_ref, 0);
6d3abfc0 376 size_t namelen = strlen (name);
ab06d2ff 377 machopic_indirection *p;
6d3abfc0 378 void ** slot;
524eafbc 379 bool saw_star = false;
380 bool needs_quotes;
381 const char *suffix;
382 const char *prefix = user_label_prefix;
383 const char *quote = "";
b359164b 384 tree id;
385
386 id = maybe_get_identifier (name);
387 if (id)
388 {
389 tree id_orig = id;
390
391 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
392 id = TREE_CHAIN (id);
393 if (id != id_orig)
394 {
395 name = IDENTIFIER_POINTER (id);
396 namelen = strlen (name);
397 }
398 }
b215c058 399
524eafbc 400 if (name[0] == '*')
8dfaa51b 401 {
524eafbc 402 saw_star = true;
403 prefix = "";
404 ++name;
405 --namelen;
ab06d2ff 406 }
524eafbc 407
408 needs_quotes = name_needs_quotes (name);
409 if (needs_quotes)
ab06d2ff 410 {
524eafbc 411 quote = "\"";
8dfaa51b 412 }
413
524eafbc 414 if (stub_p)
415 suffix = STUB_SUFFIX;
416 else
417 suffix = NON_LAZY_POINTER_SUFFIX;
418
419 buffer = alloca (strlen ("&L")
420 + strlen (prefix)
421 + namelen
422 + strlen (suffix)
423 + 2 * strlen (quote)
424 + 1 /* '\0' */);
425
426 /* Construct the name of the non-lazy pointer or stub. */
427 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
428
6d3abfc0 429 if (!machopic_indirections)
b215c058 430 machopic_indirections = htab_create_ggc (37,
6d3abfc0 431 machopic_indirection_hash,
432 machopic_indirection_eq,
433 /*htab_del=*/NULL);
b215c058 434
6d3abfc0 435 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
436 htab_hash_string (buffer), INSERT);
437 if (*slot)
438 {
439 p = (machopic_indirection *) *slot;
440 }
441 else
ab06d2ff 442 {
ab06d2ff 443 p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection));
444 p->symbol = sym_ref;
6d3abfc0 445 p->ptr_name = xstrdup (buffer);
ab06d2ff 446 p->stub_p = stub_p;
6d3abfc0 447 p->used = false;
448 *slot = p;
ab06d2ff 449 }
b215c058 450
6d3abfc0 451 return p->ptr_name;
80d725d7 452}
453
ab06d2ff 454/* Return the name of the stub for the mcount function. */
80d725d7 455
ab06d2ff 456const char*
457machopic_mcount_stub_name (void)
80d725d7 458{
620dd87a 459 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
460 return machopic_indirection_name (symbol, /*stub_p=*/true);
80d725d7 461}
462
ab06d2ff 463/* If NAME is the name of a stub or a non-lazy pointer , mark the stub
464 or non-lazy pointer as used -- and mark the object to which the
465 pointer/stub refers as used as well, since the pointer/stub will
466 emit a reference to it. */
467
80d725d7 468void
ab06d2ff 469machopic_validate_stub_or_non_lazy_ptr (const char *name)
80d725d7 470{
ab06d2ff 471 machopic_indirection *p;
b215c058 472
473 p = ((machopic_indirection *)
6d3abfc0 474 (htab_find_with_hash (machopic_indirections, name,
475 htab_hash_string (name))));
476 if (p && ! p->used)
ab06d2ff 477 {
6d3abfc0 478 const char *real_name;
479 tree id;
b215c058 480
6d3abfc0 481 p->used = true;
482
09e39ca0 483 /* Do what output_addr_const will do when we actually call it. */
484 if (SYMBOL_REF_DECL (p->symbol))
485 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
486
487 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
b215c058 488
6d3abfc0 489 id = maybe_get_identifier (real_name);
490 if (id)
491 mark_referenced (id);
ab06d2ff 492 }
80d725d7 493}
494
495/* Transform ORIG, which may be any data source, to the corresponding
496 source using indirections. */
497
498rtx
b40da9a7 499machopic_indirect_data_reference (rtx orig, rtx reg)
80d725d7 500{
501 rtx ptr_ref = orig;
b40da9a7 502
80d725d7 503 if (! MACHOPIC_INDIRECT)
504 return orig;
505
506 if (GET_CODE (orig) == SYMBOL_REF)
507 {
ab06d2ff 508 int defined = machopic_data_defined_p (orig);
8f74ae7b 509
510 if (defined && MACHO_DYNAMIC_NO_PIC_P)
511 {
512#if defined (TARGET_TOC)
fed0a860 513 /* Create a new register for CSE opportunities. */
514 rtx hi_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
515 emit_insn (gen_macho_high (hi_reg, orig));
516 emit_insn (gen_macho_low (reg, hi_reg, orig));
8f74ae7b 517#else
518 /* some other cpu -- writeme! */
27d2baef 519 gcc_unreachable ();
8f74ae7b 520#endif
521 return reg;
522 }
523 else if (defined)
80d725d7 524 {
5352873f 525#if defined (TARGET_TOC) || defined (HAVE_lo_sum)
ab06d2ff 526 rtx pic_base = machopic_function_base_sym ();
b06a3cc3 527 rtx offset = gen_pic_offset (orig, pic_base);
5352873f 528#endif
80d725d7 529
530#if defined (TARGET_TOC) /* i.e., PowerPC */
64668281 531 rtx hi_sum_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
80d725d7 532
27d2baef 533 gcc_assert (reg);
80d725d7 534
37ec5024 535 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
536 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
537 gen_rtx_HIGH (Pmode, offset))));
538 emit_insn (gen_rtx_SET (Pmode, reg,
539 gen_rtx_LO_SUM (Pmode, hi_sum_reg, offset)));
80d725d7 540
541 orig = reg;
542#else
543#if defined (HAVE_lo_sum)
27d2baef 544 gcc_assert (reg);
80d725d7 545
37ec5024 546 emit_insn (gen_rtx_SET (VOIDmode, reg,
547 gen_rtx_HIGH (Pmode, offset)));
548 emit_insn (gen_rtx_SET (VOIDmode, reg,
549 gen_rtx_LO_SUM (Pmode, reg, offset)));
550 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
80d725d7 551
37ec5024 552 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
80d725d7 553#endif
554#endif
555 return orig;
556 }
557
329f67ae 558 ptr_ref = (gen_rtx_SYMBOL_REF
b215c058 559 (Pmode,
ab06d2ff 560 machopic_indirection_name (orig, /*stub_p=*/false)));
80d725d7 561
e265a6da 562 SYMBOL_REF_DECL (ptr_ref) = SYMBOL_REF_DECL (orig);
f9226418 563
e265a6da 564 ptr_ref = gen_const_mem (Pmode, ptr_ref);
329f67ae 565 machopic_define_symbol (ptr_ref);
80d725d7 566
567 return ptr_ref;
568 }
569 else if (GET_CODE (orig) == CONST)
570 {
571 rtx base, result;
572
573 /* legitimize both operands of the PLUS */
574 if (GET_CODE (XEXP (orig, 0)) == PLUS)
575 {
576 base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0),
577 reg);
578 orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1),
579 (base == reg ? 0 : reg));
580 }
b40da9a7 581 else
80d725d7 582 return orig;
583
584 if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
b244d4c7 585 result = plus_constant (base, INTVAL (orig));
80d725d7 586 else
37ec5024 587 result = gen_rtx_PLUS (Pmode, base, orig);
80d725d7 588
80d725d7 589 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
590 {
591 if (reg)
592 {
593 emit_move_insn (reg, result);
594 result = reg;
595 }
596 else
597 {
598 result = force_reg (GET_MODE (result), result);
599 }
600 }
601
602 return result;
603
604 }
605 else if (GET_CODE (orig) == MEM)
606 XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg);
607 /* When the target is i386, this code prevents crashes due to the
608 compiler's ignorance on how to move the PIC base register to
609 other registers. (The reload phase sometimes introduces such
610 insns.) */
611 else if (GET_CODE (orig) == PLUS
612 && GET_CODE (XEXP (orig, 0)) == REG
613 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
614#ifdef I386
615 /* Prevent the same register from being erroneously used
616 as both the base and index registers. */
617 && GET_CODE (XEXP (orig, 1)) == CONST
618#endif
619 && reg)
620 {
621 emit_move_insn (reg, XEXP (orig, 0));
622 XEXP (ptr_ref, 0) = reg;
623 }
624 return ptr_ref;
625}
626
80d725d7 627/* Transform TARGET (a MEM), which is a function call target, to the
628 corresponding symbol_stub if necessary. Return a new MEM. */
629
630rtx
b40da9a7 631machopic_indirect_call_target (rtx target)
80d725d7 632{
633 if (GET_CODE (target) != MEM)
634 return target;
635
b215c058 636 if (MACHOPIC_INDIRECT
ab06d2ff 637 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
638 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
639 & MACHO_SYMBOL_FLAG_DEFINED))
b40da9a7 640 {
ab06d2ff 641 rtx sym_ref = XEXP (target, 0);
b215c058 642 const char *stub_name = machopic_indirection_name (sym_ref,
ab06d2ff 643 /*stub_p=*/true);
644 enum machine_mode mode = GET_MODE (sym_ref);
645 tree decl = SYMBOL_REF_DECL (sym_ref);
b215c058 646
ab06d2ff 647 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
648 SYMBOL_REF_DECL (XEXP (target, 0)) = decl;
b04fab2a 649 MEM_READONLY_P (target) = 1;
e265a6da 650 MEM_NOTRAP_P (target) = 1;
80d725d7 651 }
652
653 return target;
654}
655
656rtx
b40da9a7 657machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
80d725d7 658{
659 rtx pic_ref = orig;
660
8f74ae7b 661 if (! MACHOPIC_INDIRECT)
80d725d7 662 return orig;
663
664 /* First handle a simple SYMBOL_REF or LABEL_REF */
665 if (GET_CODE (orig) == LABEL_REF
666 || (GET_CODE (orig) == SYMBOL_REF
667 ))
668 {
669 /* addr(foo) = &func+(foo-func) */
670 rtx pic_base;
671
672 orig = machopic_indirect_data_reference (orig, reg);
673
b40da9a7 674 if (GET_CODE (orig) == PLUS
80d725d7 675 && GET_CODE (XEXP (orig, 0)) == REG)
676 {
677 if (reg == 0)
678 return force_reg (mode, orig);
679
680 emit_move_insn (reg, orig);
681 return reg;
b40da9a7 682 }
80d725d7 683
b06a3cc3 684 /* if dynamic-no-pic we don't have a pic base */
8f74ae7b 685 if (MACHO_DYNAMIC_NO_PIC_P)
b06a3cc3 686 pic_base = NULL;
8f74ae7b 687 else
ab06d2ff 688 pic_base = machopic_function_base_sym ();
80d725d7 689
690 if (GET_CODE (orig) == MEM)
691 {
692 if (reg == 0)
693 {
27d2baef 694 gcc_assert (!reload_in_progress);
695 reg = gen_reg_rtx (Pmode);
80d725d7 696 }
b40da9a7 697
80d725d7 698#ifdef HAVE_lo_sum
8f74ae7b 699 if (MACHO_DYNAMIC_NO_PIC_P
700 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
701 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
702 {
703#if defined (TARGET_TOC) /* ppc */
704 rtx temp_reg = (no_new_pseudos) ? reg : gen_reg_rtx (Pmode);
705 rtx asym = XEXP (orig, 0);
706 rtx mem;
707
301a5e52 708 emit_insn (gen_macho_high (temp_reg, asym));
e265a6da 709 mem = gen_const_mem (GET_MODE (orig),
710 gen_rtx_LO_SUM (Pmode, temp_reg, asym));
37ec5024 711 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
8f74ae7b 712#else
713 /* Some other CPU -- WriteMe! but right now there are no other platform that can use dynamic-no-pic */
27d2baef 714 gcc_unreachable ();
8f74ae7b 715#endif
716 pic_ref = reg;
717 }
718 else
b40da9a7 719 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
80d725d7 720 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
721 {
b06a3cc3 722 rtx offset = gen_pic_offset (XEXP (orig, 0), pic_base);
80d725d7 723#if defined (TARGET_TOC) /* i.e., PowerPC */
724 /* Generating a new reg may expose opportunities for
725 common subexpression elimination. */
09c6aad0 726 rtx hi_sum_reg = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
37ec5024 727 rtx mem;
728 rtx insn;
729 rtx sum;
b215c058 730
37ec5024 731 sum = gen_rtx_HIGH (Pmode, offset);
732 if (! MACHO_DYNAMIC_NO_PIC_P)
733 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
80d725d7 734
37ec5024 735 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
736
e265a6da 737 mem = gen_const_mem (GET_MODE (orig),
b215c058 738 gen_rtx_LO_SUM (Pmode,
e265a6da 739 hi_sum_reg, offset));
37ec5024 740 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
b215c058 741 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, pic_ref,
37ec5024 742 REG_NOTES (insn));
743
744 pic_ref = reg;
80d725d7 745#else
37ec5024 746 emit_insn (gen_rtx_USE (VOIDmode,
b215c058 747 gen_rtx_REG (Pmode,
37ec5024 748 PIC_OFFSET_TABLE_REGNUM)));
749
750 emit_insn (gen_rtx_SET (VOIDmode, reg,
751 gen_rtx_HIGH (Pmode,
b215c058 752 gen_rtx_CONST (Pmode,
37ec5024 753 offset))));
754 emit_insn (gen_rtx_SET (VOIDmode, reg,
755 gen_rtx_LO_SUM (Pmode, reg,
756 gen_rtx_CONST (Pmode, offset))));
757 pic_ref = gen_rtx_PLUS (Pmode,
758 pic_offset_table_rtx, reg);
80d725d7 759#endif
760 }
761 else
762#endif /* HAVE_lo_sum */
763 {
764 rtx pic = pic_offset_table_rtx;
765 if (GET_CODE (pic) != REG)
766 {
767 emit_move_insn (reg, pic);
768 pic = reg;
769 }
770#if 0
37ec5024 771 emit_insn (gen_rtx_USE (VOIDmode,
b215c058 772 gen_rtx_REG (Pmode,
37ec5024 773 PIC_OFFSET_TABLE_REGNUM)));
80d725d7 774#endif
775
3e111a71 776 if (reload_in_progress)
777 regs_ever_live[REGNO (pic)] = 1;
b06a3cc3 778 pic_ref = gen_rtx_PLUS (Pmode, pic,
779 gen_pic_offset (XEXP (orig, 0),
780 pic_base));
80d725d7 781 }
b40da9a7 782
80d725d7 783#if !defined (TARGET_TOC)
80d725d7 784 emit_move_insn (reg, pic_ref);
e265a6da 785 pic_ref = gen_const_mem (GET_MODE (orig), reg);
7ba97778 786#endif
80d725d7 787 }
788 else
789 {
790
791#ifdef HAVE_lo_sum
b40da9a7 792 if (GET_CODE (orig) == SYMBOL_REF
80d725d7 793 || GET_CODE (orig) == LABEL_REF)
794 {
b06a3cc3 795 rtx offset = gen_pic_offset (orig, pic_base);
80d725d7 796#if defined (TARGET_TOC) /* i.e., PowerPC */
797 rtx hi_sum_reg;
798
799 if (reg == 0)
800 {
27d2baef 801 gcc_assert (!reload_in_progress);
802 reg = gen_reg_rtx (Pmode);
80d725d7 803 }
b40da9a7 804
80d725d7 805 hi_sum_reg = reg;
806
37ec5024 807 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
808 (MACHO_DYNAMIC_NO_PIC_P)
809 ? gen_rtx_HIGH (Pmode, offset)
810 : gen_rtx_PLUS (Pmode,
811 pic_offset_table_rtx,
b215c058 812 gen_rtx_HIGH (Pmode,
37ec5024 813 offset))));
814 emit_insn (gen_rtx_SET (VOIDmode, reg,
815 gen_rtx_LO_SUM (Pmode,
816 hi_sum_reg, offset)));
80d725d7 817 pic_ref = reg;
818#else
37ec5024 819 emit_insn (gen_rtx_SET (VOIDmode, reg,
820 gen_rtx_HIGH (Pmode, offset)));
821 emit_insn (gen_rtx_SET (VOIDmode, reg,
822 gen_rtx_LO_SUM (Pmode, reg, offset)));
823 pic_ref = gen_rtx_PLUS (Pmode,
824 pic_offset_table_rtx, reg);
80d725d7 825#endif
826 }
827 else
828#endif /* HAVE_lo_sum */
829 {
7a5893fc 830 if (REG_P (orig)
831 || GET_CODE (orig) == SUBREG)
80d725d7 832 {
833 return orig;
834 }
835 else
836 {
837 rtx pic = pic_offset_table_rtx;
838 if (GET_CODE (pic) != REG)
839 {
840 emit_move_insn (reg, pic);
841 pic = reg;
842 }
843#if 0
37ec5024 844 emit_insn (gen_rtx_USE (VOIDmode,
845 pic_offset_table_rtx));
80d725d7 846#endif
3e111a71 847 if (reload_in_progress)
848 regs_ever_live[REGNO (pic)] = 1;
37ec5024 849 pic_ref = gen_rtx_PLUS (Pmode,
850 pic,
b06a3cc3 851 gen_pic_offset (orig, pic_base));
80d725d7 852 }
853 }
854 }
855
80d725d7 856 if (GET_CODE (pic_ref) != REG)
857 {
858 if (reg != 0)
859 {
860 emit_move_insn (reg, pic_ref);
861 return reg;
862 }
863 else
864 {
865 return force_reg (mode, pic_ref);
866 }
867 }
868 else
869 {
870 return pic_ref;
871 }
872 }
873
874 else if (GET_CODE (orig) == SYMBOL_REF)
875 return orig;
876
877 else if (GET_CODE (orig) == PLUS
878 && (GET_CODE (XEXP (orig, 0)) == MEM
879 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
880 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
881 && XEXP (orig, 0) != pic_offset_table_rtx
882 && GET_CODE (XEXP (orig, 1)) != REG)
b40da9a7 883
80d725d7 884 {
885 rtx base;
886 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
887
888 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
889 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
890 Pmode, (base == reg ? 0 : reg));
891 if (GET_CODE (orig) == CONST_INT)
892 {
b244d4c7 893 pic_ref = plus_constant (base, INTVAL (orig));
80d725d7 894 is_complex = 1;
895 }
896 else
37ec5024 897 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
80d725d7 898
80d725d7 899 if (reg && is_complex)
900 {
901 emit_move_insn (reg, pic_ref);
902 pic_ref = reg;
903 }
904 /* Likewise, should we set special REG_NOTEs here? */
905 }
906
907 else if (GET_CODE (orig) == CONST)
908 {
909 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
910 }
911
912 else if (GET_CODE (orig) == MEM
913 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
914 {
915 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
b04fab2a 916 addr = replace_equiv_address (orig, addr);
80d725d7 917 emit_move_insn (reg, addr);
918 pic_ref = reg;
919 }
920
921 return pic_ref;
922}
923
ab06d2ff 924/* Output the stub or non-lazy pointer in *SLOT, if it has been used.
925 DATA is the FILE* for assembly output. Called from
926 htab_traverse. */
80d725d7 927
ab06d2ff 928static int
929machopic_output_indirection (void **slot, void *data)
80d725d7 930{
ab06d2ff 931 machopic_indirection *p = *((machopic_indirection **) slot);
932 FILE *asm_out_file = (FILE *) data;
933 rtx symbol;
934 const char *sym_name;
935 const char *ptr_name;
b215c058 936
ab06d2ff 937 if (!p->used)
938 return 1;
80d725d7 939
ab06d2ff 940 symbol = p->symbol;
941 sym_name = XSTR (symbol, 0);
6d3abfc0 942 ptr_name = p->ptr_name;
b215c058 943
ab06d2ff 944 if (p->stub_p)
80d725d7 945 {
80d725d7 946 char *sym;
947 char *stub;
b359164b 948 tree id;
949
950 id = maybe_get_identifier (sym_name);
951 if (id)
952 {
953 tree id_orig = id;
954
955 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
956 id = TREE_CHAIN (id);
957 if (id != id_orig)
958 sym_name = IDENTIFIER_POINTER (id);
959 }
80d725d7 960
80d725d7 961 sym = alloca (strlen (sym_name) + 2);
962 if (sym_name[0] == '*' || sym_name[0] == '&')
963 strcpy (sym, sym_name + 1);
964 else if (sym_name[0] == '-' || sym_name[0] == '+')
b40da9a7 965 strcpy (sym, sym_name);
80d725d7 966 else
e9764bbb 967 sprintf (sym, "%s%s", user_label_prefix, sym_name);
80d725d7 968
ab06d2ff 969 stub = alloca (strlen (ptr_name) + 2);
970 if (ptr_name[0] == '*' || ptr_name[0] == '&')
971 strcpy (stub, ptr_name + 1);
80d725d7 972 else
ab06d2ff 973 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
80d725d7 974
09e39ca0 975 machopic_output_stub (asm_out_file, sym, stub);
80d725d7 976 }
f0ed0e8a 977 else if (! indirect_data (symbol)
8d806159 978 && (machopic_symbol_defined_p (symbol)
979 || SYMBOL_REF_LOCAL_P (symbol)))
80d725d7 980 {
2f14b1f9 981 switch_to_section (data_section);
ab06d2ff 982 assemble_align (GET_MODE_ALIGNMENT (Pmode));
983 assemble_label (ptr_name);
984 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
985 GET_MODE_SIZE (Pmode),
986 GET_MODE_ALIGNMENT (Pmode), 1);
80d725d7 987 }
ab06d2ff 988 else
989 {
f0ed0e8a 990 rtx init = const0_rtx;
991
92e4ba7d 992 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
ab06d2ff 993 assemble_name (asm_out_file, ptr_name);
994 fprintf (asm_out_file, ":\n");
b215c058 995
ab06d2ff 996 fprintf (asm_out_file, "\t.indirect_symbol ");
997 assemble_name (asm_out_file, sym_name);
998 fprintf (asm_out_file, "\n");
b215c058 999
f0ed0e8a 1000 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1001 have their symbol name instead of 0 in the second entry of
1002 the non-lazy symbol pointer data structure when they are
1003 defined. This allows the runtime to rebind newer instances
1004 of the translation unit with the original instance of the
d188633a 1005 symbol. */
f0ed0e8a 1006
1007 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1008 && machopic_symbol_defined_p (symbol))
1009 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1010
1011 assemble_integer (init, GET_MODE_SIZE (Pmode),
ab06d2ff 1012 GET_MODE_ALIGNMENT (Pmode), 1);
1013 }
b215c058 1014
ab06d2ff 1015 return 1;
1016}
1017
1018void
1019machopic_finish (FILE *asm_out_file)
1020{
1021 if (machopic_indirections)
f0ed0e8a 1022 htab_traverse_noresize (machopic_indirections,
ab06d2ff 1023 machopic_output_indirection,
1024 asm_out_file);
80d725d7 1025}
1026
b40da9a7 1027int
1028machopic_operand_p (rtx op)
80d725d7 1029{
1030 if (MACHOPIC_JUST_INDIRECT)
1031 {
1032 while (GET_CODE (op) == CONST)
1033 op = XEXP (op, 0);
1034
1035 if (GET_CODE (op) == SYMBOL_REF)
ab06d2ff 1036 return machopic_symbol_defined_p (op);
80d725d7 1037 else
1038 return 0;
1039 }
1040
1041 while (GET_CODE (op) == CONST)
1042 op = XEXP (op, 0);
1043
1044 if (GET_CODE (op) == MINUS
1045 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1046 && GET_CODE (XEXP (op, 1)) == SYMBOL_REF
ab06d2ff 1047 && machopic_symbol_defined_p (XEXP (op, 0))
1048 && machopic_symbol_defined_p (XEXP (op, 1)))
80d725d7 1049 return 1;
1050
80d725d7 1051 return 0;
1052}
8dfaa51b 1053
1054/* This function records whether a given name corresponds to a defined
1055 or undefined function or variable, for machopic_classify_ident to
1056 use later. */
1057
1058void
b40da9a7 1059darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
8dfaa51b 1060{
5a121a91 1061 rtx sym_ref;
8dfaa51b 1062
9af9a32c 1063 /* Do the standard encoding things first. */
1064 default_encode_section_info (decl, rtl, first);
1065
ab06d2ff 1066 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1067 return;
9af9a32c 1068
ab06d2ff 1069 sym_ref = XEXP (rtl, 0);
1070 if (TREE_CODE (decl) == VAR_DECL)
1071 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1072
1073 if (!DECL_EXTERNAL (decl)
1dc74225 1074 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
0a3ecdc1 1075 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
8dfaa51b 1076 && ((TREE_STATIC (decl)
1077 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
2f4fae92 1078 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
fd419242 1079 && DECL_INITIAL (decl) != error_mark_node)))
ab06d2ff 1080 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
f0ed0e8a 1081
d188633a 1082 if (! TREE_PUBLIC (decl))
f0ed0e8a 1083 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
e479d0de 1084}
01d15dc5 1085
9423c9b7 1086void
1087darwin_mark_decl_preserved (const char *name)
1088{
1089 fprintf (asm_out_file, ".no_dead_strip ");
1090 assemble_name (asm_out_file, name);
1091 fputc ('\n', asm_out_file);
1092}
1093
2f14b1f9 1094section *
b40da9a7 1095machopic_select_section (tree exp, int reloc,
1096 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
52470889 1097{
2f14b1f9 1098 section *base_section;
ba83457d 1099 bool weak_p = (DECL_P (exp) && DECL_WEAK (exp)
1100 && (lookup_attribute ("weak", DECL_ATTRIBUTES (exp))
1101 || ! lookup_attribute ("weak_import",
1102 DECL_ATTRIBUTES (exp))));
2f14b1f9 1103
1dc74225 1104 if (TREE_CODE (exp) == FUNCTION_DECL)
2f14b1f9 1105 {
5fbee89d 1106 if (reloc == 1)
2f14b1f9 1107 base_section = (weak_p
92e4ba7d 1108 ? darwin_sections[text_unlikely_coal_section]
2f14b1f9 1109 : unlikely_text_section ());
1110 else
92e4ba7d 1111 base_section = weak_p ? darwin_sections[text_coal_section] : text_section;
2f14b1f9 1112 }
1dc74225 1113 else if (decl_readonly_section_1 (exp, reloc, MACHOPIC_INDIRECT))
92e4ba7d 1114 base_section = weak_p ? darwin_sections[const_coal_section] : darwin_sections[const_section];
7def0f5c 1115 else if (TREE_READONLY (exp) || TREE_CONSTANT (exp))
92e4ba7d 1116 base_section = weak_p ? darwin_sections[const_data_coal_section] : darwin_sections[const_data_section];
7def0f5c 1117 else
92e4ba7d 1118 base_section = weak_p ? darwin_sections[data_coal_section] : data_section;
b40da9a7 1119
7def0f5c 1120 if (TREE_CODE (exp) == STRING_CST
b40da9a7 1121 && ((size_t) TREE_STRING_LENGTH (exp)
a814bad5 1122 == strlen (TREE_STRING_POINTER (exp)) + 1))
92e4ba7d 1123 return darwin_sections[cstring_section];
7def0f5c 1124 else if ((TREE_CODE (exp) == INTEGER_CST || TREE_CODE (exp) == REAL_CST)
1125 && flag_merge_constants)
52470889 1126 {
853b103d 1127 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
52470889 1128
1129 if (TREE_CODE (size) == INTEGER_CST &&
1130 TREE_INT_CST_LOW (size) == 4 &&
1131 TREE_INT_CST_HIGH (size) == 0)
92e4ba7d 1132 return darwin_sections[literal4_section];
52470889 1133 else if (TREE_CODE (size) == INTEGER_CST &&
1134 TREE_INT_CST_LOW (size) == 8 &&
1135 TREE_INT_CST_HIGH (size) == 0)
92e4ba7d 1136 return darwin_sections[literal8_section];
52470889 1137 else
2f14b1f9 1138 return base_section;
52470889 1139 }
1140 else if (TREE_CODE (exp) == CONSTRUCTOR
1141 && TREE_TYPE (exp)
1142 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
1143 && TYPE_NAME (TREE_TYPE (exp)))
1144 {
1145 tree name = TYPE_NAME (TREE_TYPE (exp));
1146 if (TREE_CODE (name) == TYPE_DECL)
1147 name = DECL_NAME (name);
0616a948 1148
1149 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1150 {
1151 if (flag_next_runtime)
92e4ba7d 1152 return darwin_sections[objc_constant_string_object_section];
0616a948 1153 else
92e4ba7d 1154 return darwin_sections[objc_string_object_section];
0616a948 1155 }
52470889 1156 else
2f14b1f9 1157 return base_section;
52470889 1158 }
1159 else if (TREE_CODE (exp) == VAR_DECL &&
1160 DECL_NAME (exp) &&
1161 TREE_CODE (DECL_NAME (exp)) == IDENTIFIER_NODE &&
1162 IDENTIFIER_POINTER (DECL_NAME (exp)) &&
1163 !strncmp (IDENTIFIER_POINTER (DECL_NAME (exp)), "_OBJC_", 6))
1164 {
1165 const char *name = IDENTIFIER_POINTER (DECL_NAME (exp));
1166
1167 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
92e4ba7d 1168 return darwin_sections[objc_cls_meth_section];
52470889 1169 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
92e4ba7d 1170 return darwin_sections[objc_inst_meth_section];
52470889 1171 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20))
92e4ba7d 1172 return darwin_sections[objc_cat_cls_meth_section];
52470889 1173 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23))
92e4ba7d 1174 return darwin_sections[objc_cat_inst_meth_section];
52470889 1175 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
92e4ba7d 1176 return darwin_sections[objc_class_vars_section];
52470889 1177 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
92e4ba7d 1178 return darwin_sections[objc_instance_vars_section];
52470889 1179 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
92e4ba7d 1180 return darwin_sections[objc_cat_cls_meth_section];
52470889 1181 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
92e4ba7d 1182 return darwin_sections[objc_class_names_section];
52470889 1183 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
92e4ba7d 1184 return darwin_sections[objc_meth_var_names_section];
52470889 1185 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
92e4ba7d 1186 return darwin_sections[objc_meth_var_types_section];
52470889 1187 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
92e4ba7d 1188 return darwin_sections[objc_cls_refs_section];
52470889 1189 else if (!strncmp (name, "_OBJC_CLASS_", 12))
92e4ba7d 1190 return darwin_sections[objc_class_section];
52470889 1191 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
92e4ba7d 1192 return darwin_sections[objc_meta_class_section];
52470889 1193 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
92e4ba7d 1194 return darwin_sections[objc_category_section];
52470889 1195 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
92e4ba7d 1196 return darwin_sections[objc_selector_refs_section];
52470889 1197 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
92e4ba7d 1198 return darwin_sections[objc_selector_fixup_section];
52470889 1199 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
92e4ba7d 1200 return darwin_sections[objc_symbols_section];
52470889 1201 else if (!strncmp (name, "_OBJC_MODULES", 13))
92e4ba7d 1202 return darwin_sections[objc_module_info_section];
c17b85ea 1203 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
92e4ba7d 1204 return darwin_sections[objc_image_info_section];
52470889 1205 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
92e4ba7d 1206 return darwin_sections[objc_cat_inst_meth_section];
52470889 1207 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
92e4ba7d 1208 return darwin_sections[objc_cat_cls_meth_section];
52470889 1209 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
92e4ba7d 1210 return darwin_sections[objc_cat_cls_meth_section];
52470889 1211 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
92e4ba7d 1212 return darwin_sections[objc_protocol_section];
52470889 1213 else
2f14b1f9 1214 return base_section;
52470889 1215 }
1216 else
2f14b1f9 1217 return base_section;
52470889 1218}
1219
bbfbe351 1220/* This can be called with address expressions as "rtx".
9cb8e99f 1221 They must go in "const". */
bbfbe351 1222
2f14b1f9 1223section *
b40da9a7 1224machopic_select_rtx_section (enum machine_mode mode, rtx x,
1225 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
bbfbe351 1226{
f2e8796e 1227 if (GET_MODE_SIZE (mode) == 8
1228 && (GET_CODE (x) == CONST_INT
1229 || GET_CODE (x) == CONST_DOUBLE))
92e4ba7d 1230 return darwin_sections[literal8_section];
bbfbe351 1231 else if (GET_MODE_SIZE (mode) == 4
1232 && (GET_CODE (x) == CONST_INT
1233 || GET_CODE (x) == CONST_DOUBLE))
92e4ba7d 1234 return darwin_sections[literal4_section];
459f8624 1235 else if (MACHOPIC_INDIRECT
76dece62 1236 && (GET_CODE (x) == SYMBOL_REF
1237 || GET_CODE (x) == CONST
1238 || GET_CODE (x) == LABEL_REF))
92e4ba7d 1239 return darwin_sections[const_data_section];
bbfbe351 1240 else
92e4ba7d 1241 return darwin_sections[const_section];
bbfbe351 1242}
1243
01d15dc5 1244void
b40da9a7 1245machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
01d15dc5 1246{
8f74ae7b 1247 if (MACHOPIC_INDIRECT)
92e4ba7d 1248 switch_to_section (darwin_sections[mod_init_section]);
01d15dc5 1249 else
92e4ba7d 1250 switch_to_section (darwin_sections[constructor_section]);
09d688ff 1251 assemble_align (POINTER_SIZE);
1252 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
01d15dc5 1253
8f74ae7b 1254 if (! MACHOPIC_INDIRECT)
01d15dc5 1255 fprintf (asm_out_file, ".reference .constructors_used\n");
1256}
1257
1258void
b40da9a7 1259machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
01d15dc5 1260{
8f74ae7b 1261 if (MACHOPIC_INDIRECT)
92e4ba7d 1262 switch_to_section (darwin_sections[mod_term_section]);
01d15dc5 1263 else
92e4ba7d 1264 switch_to_section (darwin_sections[destructor_section]);
09d688ff 1265 assemble_align (POINTER_SIZE);
1266 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
01d15dc5 1267
8f74ae7b 1268 if (! MACHOPIC_INDIRECT)
01d15dc5 1269 fprintf (asm_out_file, ".reference .destructors_used\n");
1270}
1f3233d1 1271
67c1e638 1272void
b40da9a7 1273darwin_globalize_label (FILE *stream, const char *name)
67c1e638 1274{
1275 if (!!strncmp (name, "_OBJC_", 6))
1276 default_globalize_label (stream, name);
1277}
1278
2f9fc8ef 1279void
b215c058 1280darwin_asm_named_section (const char *name,
537cd941 1281 unsigned int flags ATTRIBUTE_UNUSED,
1282 tree decl ATTRIBUTE_UNUSED)
2f9fc8ef 1283{
1dc74225 1284 fprintf (asm_out_file, "\t.section %s\n", name);
2f9fc8ef 1285}
1286
b215c058 1287void
1dc74225 1288darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
2f9fc8ef 1289{
1dc74225 1290 /* Darwin does not use unique sections. */
2f9fc8ef 1291}
1292
975d636c 1293/* Handle a "weak_import" attribute; arguments as in
1294 struct attribute_spec.handler. */
1295
1296tree
1297darwin_handle_weak_import_attribute (tree *node, tree name,
1298 tree ARG_UNUSED (args),
1299 int ARG_UNUSED (flags),
1300 bool * no_add_attrs)
1301{
d0ddd548 1302 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
975d636c 1303 {
9b2d6d13 1304 warning (OPT_Wattributes, "%qs attribute ignored",
1305 IDENTIFIER_POINTER (name));
975d636c 1306 *no_add_attrs = true;
1307 }
1308 else
1309 declare_weak (*node);
1310
1311 return NULL_TREE;
1312}
a3952a36 1313
1314static void
1315no_dead_strip (FILE *file, const char *lab)
1316{
975d636c 1317 fprintf (file, ".no_dead_strip %s\n", lab);
a3952a36 1318}
1319
b215c058 1320/* Emit a label for an FDE, making it global and/or weak if appropriate.
ef1074f7 1321 The third parameter is nonzero if this is for exception handling.
1322 The fourth parameter is nonzero if this is just a placeholder for an
2f9fc8ef 1323 FDE that we are omitting. */
ef1074f7 1324
b215c058 1325void
ef1074f7 1326darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2f9fc8ef 1327{
1328 tree id = DECL_ASSEMBLER_NAME (decl)
1329 ? DECL_ASSEMBLER_NAME (decl)
1330 : DECL_NAME (decl);
1331
524eafbc 1332 const char *prefix = user_label_prefix;
2f9fc8ef 1333
1334 const char *base = IDENTIFIER_POINTER (id);
1335 unsigned int base_len = IDENTIFIER_LENGTH (id);
1336
1337 const char *suffix = ".eh";
2f9fc8ef 1338
1339 int need_quotes = name_needs_quotes (base);
1340 int quotes_len = need_quotes ? 2 : 0;
ef1074f7 1341 char *lab;
1342
1343 if (! for_eh)
1344 suffix = ".eh1";
2f9fc8ef 1345
524eafbc 1346 lab = xmalloc (strlen (prefix)
1347 + base_len + strlen (suffix) + quotes_len + 1);
2f9fc8ef 1348 lab[0] = '\0';
1349
1350 if (need_quotes)
1351 strcat(lab, "\"");
1352 strcat(lab, prefix);
1353 strcat(lab, base);
1354 strcat(lab, suffix);
1355 if (need_quotes)
1356 strcat(lab, "\"");
1357
1358 if (TREE_PUBLIC (decl))
1dc74225 1359 fprintf (file, "\t%s %s\n",
2f9fc8ef 1360 (DECL_VISIBILITY (decl) != VISIBILITY_HIDDEN
1361 ? ".globl"
1362 : ".private_extern"),
1363 lab);
1364
1dc74225 1365 if (DECL_WEAK (decl))
1366 fprintf (file, "\t.weak_definition %s\n", lab);
2f9fc8ef 1367
1368 if (empty)
a3952a36 1369 {
1370 fprintf (file, "%s = 0\n", lab);
1371
1372 /* Mark the absolute .eh and .eh1 style labels as needed to
1373 ensure that we don't dead code strip them and keep such
1374 labels from another instantiation point until we can fix this
1375 properly with group comdat support. */
1376 no_dead_strip (file, lab);
1377 }
2f9fc8ef 1378 else
1379 fprintf (file, "%s:\n", lab);
1380
1381 free (lab);
1382}
1383
b215c058 1384static GTY(()) unsigned long except_table_label_num;
1385
1386void
1387darwin_emit_except_table_label (FILE *file)
1388{
1389 char section_start_label[30];
1390
1391 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
1392 except_table_label_num++);
1393 ASM_OUTPUT_LABEL (file, section_start_label);
1394}
1395/* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
ef1074f7 1396
2f9fc8ef 1397void
1398darwin_non_lazy_pcrel (FILE *file, rtx addr)
1399{
2f9fc8ef 1400 const char *nlp_name;
1401
27d2baef 1402 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2f9fc8ef 1403
ab06d2ff 1404 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2f9fc8ef 1405 fputs ("\t.long\t", file);
1406 ASM_OUTPUT_LABELREF (file, nlp_name);
1407 fputs ("-.", file);
1408}
1409
2c5b6111 1410/* Emit an assembler directive to set visibility for a symbol. The
1411 only supported visibilities are VISIBILITY_DEFAULT and
1412 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
1413 extern". There is no MACH-O equivalent of ELF's
1414 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
1415
b215c058 1416void
2c5b6111 1417darwin_assemble_visibility (tree decl, int vis)
1418{
1419 if (vis == VISIBILITY_DEFAULT)
1420 ;
1421 else if (vis == VISIBILITY_HIDDEN)
1422 {
1423 fputs ("\t.private_extern ", asm_out_file);
1424 assemble_name (asm_out_file,
1425 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
1426 fputs ("\n", asm_out_file);
1427 }
1428 else
9b2d6d13 1429 warning (OPT_Wattributes, "internal and protected visibility attributes "
1430 "not supported in this configuration; ignored");
2c5b6111 1431}
1432
a08b74c8 1433/* Output a difference of two labels that will be an assembly time
1434 constant if the two labels are local. (.long lab1-lab2 will be
1435 very different if lab1 is at the boundary between two sections; it
1436 will be relocated according to the second section, not the first,
1437 so one ends up with a difference between labels in different
1438 sections, which is bad in the dwarf2 eh context for instance.) */
1439
1440static int darwin_dwarf_label_counter;
1441
1442void
85d3e3b5 1443darwin_asm_output_dwarf_delta (FILE *file, int size,
b40da9a7 1444 const char *lab1, const char *lab2)
a08b74c8 1445{
2f9fc8ef 1446 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
1447 && lab2[0] == '*' && lab2[1] == 'L');
dcf3c3f7 1448 const char *directive = (size == 8 ? ".quad" : ".long");
a08b74c8 1449
1450 if (islocaldiff)
1451 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
1452 else
85d3e3b5 1453 fprintf (file, "\t%s\t", directive);
48126f0c 1454 assemble_name_raw (file, lab1);
a08b74c8 1455 fprintf (file, "-");
48126f0c 1456 assemble_name_raw (file, lab2);
a08b74c8 1457 if (islocaldiff)
85d3e3b5 1458 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
a08b74c8 1459}
1460
f6940372 1461void
b40da9a7 1462darwin_file_end (void)
f6940372 1463{
1464 machopic_finish (asm_out_file);
1465 if (strcmp (lang_hooks.name, "GNU C++") == 0)
1466 {
92e4ba7d 1467 switch_to_section (darwin_sections[constructor_section]);
1468 switch_to_section (darwin_sections[destructor_section]);
f6940372 1469 ASM_OUTPUT_ALIGN (asm_out_file, 1);
1470 }
9e96724e 1471 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
f6940372 1472}
1473
0765d519 1474/* Cross-module name binding. Darwin does not support overriding
1475 functions at dynamic-link time. */
1476
1477bool
1478darwin_binds_local_p (tree decl)
1479{
1480 return default_binds_local_p_1 (decl, 0);
1481}
1482
1f3233d1 1483#include "gt-darwin.h"