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