]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/darwin.c
Mention configure in last entry
[thirdparty/gcc.git] / gcc / config / darwin.c
CommitLineData
ee890fe2 1/* Functions for generic Darwin as target machine for GNU C compiler.
0ea5865c 2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003, 2004
ee890fe2
SS
3 Free Software Foundation, Inc.
4 Contributed by Apple Computer Inc.
5
7ec022b2 6This file is part of GCC.
ee890fe2 7
7ec022b2 8GCC is free software; you can redistribute it and/or modify
ee890fe2
SS
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
7ec022b2 13GCC is distributed in the hope that it will be useful,
ee890fe2
SS
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
7ec022b2 19along with GCC; see the file COPYING. If not, write to
ee890fe2
SS
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
22
23#include "config.h"
24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
ee890fe2
SS
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"
ee890fe2
SS
40#include "function.h"
41#include "ggc.h"
3ac88239 42#include "langhooks.h"
1adaa117 43#include "target.h"
245ff137 44#include "tm_p.h"
6ce4806b 45#include "errors.h"
11abc112 46#include "hashtab.h"
ee890fe2 47
699c914a
MS
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,
c112cf2b 66 including existing function pointers and virtual methods. See
699c914a
MS
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
8f4220dc
MA
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
ee890fe2 83int
9c808aad 84name_needs_quotes (const char *name)
ee890fe2
SS
85{
86 int c;
87 while ((c = *name++) != '\0')
6f94a68e 88 if (! ISIDNUM (c) && c != '.' && c != '$')
ee890fe2
SS
89 return 1;
90 return 0;
91}
92
9c808aad 93/*
ee890fe2
SS
94 * flag_pic = 1 ... generate only indirections
95 * flag_pic = 2 ... generate indirections and pure code
96 */
97
11abc112
MM
98static int
99machopic_symbol_defined_p (rtx sym_ref)
ee890fe2 100{
156a126c
MS
101 return (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
102 || (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref));
ee890fe2
SS
103}
104
11abc112
MM
105/* This module assumes that (const (symbol_ref "foo")) is a legal pic
106 reference, which will not be changed. */
9c808aad 107
ee890fe2 108enum machopic_addr_class
11abc112 109machopic_classify_symbol (rtx sym_ref)
ee890fe2 110{
11abc112
MM
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);
ee890fe2
SS
122}
123
699c914a
MS
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
ee890fe2 163static int
11abc112 164machopic_data_defined_p (rtx sym_ref)
ee890fe2 165{
699c914a
MS
166 if (indirect_data (sym_ref))
167 return 0;
168
11abc112 169 switch (machopic_classify_symbol (sym_ref))
ee890fe2
SS
170 {
171 case MACHOPIC_DEFINED_DATA:
172 return 1;
173 default:
174 return 0;
175 }
176}
177
ee890fe2 178void
11abc112 179machopic_define_symbol (rtx mem)
ee890fe2 180{
11abc112
MM
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;
ee890fe2
SS
186}
187
6788f5ca 188static GTY(()) char * function_base;
ee890fe2 189
92c1a778 190const char *
9c808aad 191machopic_function_base_name (void)
ee890fe2 192{
ab82a49f
AP
193 /* if dynamic-no-pic is on, we should not get here */
194 if (MACHO_DYNAMIC_NO_PIC_P)
195 abort ();
ee890fe2 196
1622229c 197 if (function_base == NULL)
9c808aad 198 function_base =
1622229c
AP
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
11abc112
MM
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
1622229c
AP
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
ff482c8d 227 /* If dynamic-no-pic is on, we should not get here. */
1622229c
AP
228 if (MACHO_DYNAMIC_NO_PIC_P)
229 abort ();
9c808aad 230 current_name =
1622229c 231 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
6788f5ca 232 if (function_base_func_name != current_name)
ee890fe2 233 {
ee890fe2 234 ++current_pic_label_num;
6788f5ca 235 function_base_func_name = current_name;
ee890fe2 236 }
1622229c 237 fprintf (file, "\"L%011d$pb\"", current_pic_label_num);
ee890fe2
SS
238}
239
11abc112
MM
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"
ee890fe2 244
11abc112 245typedef struct machopic_indirection GTY (())
ee890fe2 246{
11abc112
MM
247 /* The SYMBOL_REF for the entity referenced. */
248 rtx symbol;
1adaa117
GK
249 /* The name of the stub or non-lazy pointer. */
250 const char * ptr_name;
11abc112
MM
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;
1adaa117 270 return htab_hash_string (p->ptr_name);
11abc112 271}
9c808aad 272
11abc112
MM
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{
1adaa117 279 return strcmp (((const machopic_indirection *) slot)->ptr_name, key) == 0;
11abc112 280}
ee890fe2 281
11abc112
MM
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. */
df56a27f 284
11abc112
MM
285const char *
286machopic_indirection_name (rtx sym_ref, bool stub_p)
287{
288 char *buffer;
289 const char *name = XSTR (sym_ref, 0);
1adaa117 290 size_t namelen = strlen (name);
11abc112 291 machopic_indirection *p;
1adaa117 292 void ** slot;
11abc112
MM
293
294 /* Construct the name of the non-lazy pointer or stub. */
295 if (stub_p)
df56a27f 296 {
11abc112
MM
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)
df56a27f 305 {
11abc112
MM
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);
df56a27f 311 }
11abc112
MM
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);
df56a27f
SS
329 }
330
1adaa117
GK
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
11abc112 344 {
11abc112
MM
345 p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection));
346 p->symbol = sym_ref;
1adaa117 347 p->ptr_name = xstrdup (buffer);
11abc112 348 p->stub_p = stub_p;
1adaa117
GK
349 p->used = false;
350 *slot = p;
11abc112
MM
351 }
352
1adaa117 353 return p->ptr_name;
ee890fe2
SS
354}
355
11abc112 356/* Return the name of the stub for the mcount function. */
ee890fe2 357
11abc112
MM
358const char*
359machopic_mcount_stub_name (void)
ee890fe2 360{
76f60aa5
AP
361 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
362 return machopic_indirection_name (symbol, /*stub_p=*/true);
ee890fe2
SS
363}
364
11abc112
MM
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
ee890fe2 370void
11abc112 371machopic_validate_stub_or_non_lazy_ptr (const char *name)
ee890fe2 372{
11abc112
MM
373 machopic_indirection *p;
374
375 p = ((machopic_indirection *)
1adaa117
GK
376 (htab_find_with_hash (machopic_indirections, name,
377 htab_hash_string (name))));
378 if (p && ! p->used)
11abc112 379 {
1adaa117
GK
380 const char *real_name;
381 tree id;
382
383 p->used = true;
384
ca472546
GK
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));
1adaa117
GK
390
391 id = maybe_get_identifier (real_name);
392 if (id)
393 mark_referenced (id);
11abc112 394 }
ee890fe2
SS
395}
396
397/* Transform ORIG, which may be any data source, to the corresponding
398 source using indirections. */
399
400rtx
9c808aad 401machopic_indirect_data_reference (rtx orig, rtx reg)
ee890fe2
SS
402{
403 rtx ptr_ref = orig;
9c808aad 404
ee890fe2
SS
405 if (! MACHOPIC_INDIRECT)
406 return orig;
407
408 if (GET_CODE (orig) == SYMBOL_REF)
409 {
11abc112 410 int defined = machopic_data_defined_p (orig);
ab82a49f
AP
411
412 if (defined && MACHO_DYNAMIC_NO_PIC_P)
413 {
414#if defined (TARGET_TOC)
ac9e2cff
AP
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));
ab82a49f
AP
421#else
422 /* some other cpu -- writeme! */
423 abort ();
424#endif
425 return reg;
426 }
427 else if (defined)
ee890fe2 428 {
7ae8cf75 429#if defined (TARGET_TOC) || defined (HAVE_lo_sum)
11abc112 430 rtx pic_base = machopic_function_base_sym ();
6f94a68e
GK
431 rtx offset = gen_rtx_CONST (Pmode,
432 gen_rtx_MINUS (Pmode, orig, pic_base));
7ae8cf75 433#endif
ee890fe2
SS
434
435#if defined (TARGET_TOC) /* i.e., PowerPC */
d9b46dfb 436 rtx hi_sum_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
ee890fe2
SS
437
438 if (reg == NULL)
439 abort ();
440
6f94a68e
GK
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)));
ee890fe2
SS
446
447 orig = reg;
448#else
449#if defined (HAVE_lo_sum)
450 if (reg == 0) abort ();
451
6f94a68e
GK
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));
ee890fe2 457
6f94a68e 458 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
ee890fe2
SS
459#endif
460#endif
461 return orig;
462 }
463
828a4fe4 464 ptr_ref = (gen_rtx_SYMBOL_REF
11abc112
MM
465 (Pmode,
466 machopic_indirection_name (orig, /*stub_p=*/false)));
ee890fe2 467
542a8afa 468 SYMBOL_REF_DECL (ptr_ref) = SYMBOL_REF_DECL (orig);
c0d594f1 469
542a8afa 470 ptr_ref = gen_const_mem (Pmode, ptr_ref);
828a4fe4 471 machopic_define_symbol (ptr_ref);
ee890fe2
SS
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 }
9c808aad 487 else
ee890fe2
SS
488 return orig;
489
490 if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
ed8908e7 491 result = plus_constant (base, INTVAL (orig));
ee890fe2 492 else
6f94a68e 493 result = gen_rtx_PLUS (Pmode, base, orig);
ee890fe2 494
ee890fe2
SS
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
ee890fe2
SS
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
9c808aad 537machopic_indirect_call_target (rtx target)
ee890fe2
SS
538{
539 if (GET_CODE (target) != MEM)
540 return target;
541
11abc112
MM
542 if (MACHOPIC_INDIRECT
543 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
544 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
545 & MACHO_SYMBOL_FLAG_DEFINED))
9c808aad 546 {
11abc112
MM
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;
389fdba0 555 MEM_READONLY_P (target) = 1;
542a8afa 556 MEM_NOTRAP_P (target) = 1;
ee890fe2
SS
557 }
558
559 return target;
560}
561
562rtx
9c808aad 563machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
ee890fe2
SS
564{
565 rtx pic_ref = orig;
566
ab82a49f 567 if (! MACHOPIC_INDIRECT)
ee890fe2
SS
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
9c808aad 580 if (GET_CODE (orig) == PLUS
ee890fe2
SS
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;
9c808aad 588 }
ee890fe2 589
ab82a49f
AP
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
11abc112 594 pic_base = machopic_function_base_sym ();
ee890fe2
SS
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 }
9c808aad 605
ee890fe2 606#ifdef HAVE_lo_sum
ab82a49f
AP
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
ac9e2cff
AP
616 emit_insn (mode == DImode
617 ? gen_macho_high_di (temp_reg, asym)
618 : gen_macho_high (temp_reg, asym));
542a8afa
RH
619 mem = gen_const_mem (GET_MODE (orig),
620 gen_rtx_LO_SUM (Pmode, temp_reg, asym));
6f94a68e 621 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
ab82a49f
AP
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
9c808aad 629 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
ee890fe2
SS
630 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
631 {
6f94a68e
GK
632 rtx offset = gen_rtx_CONST (Pmode,
633 gen_rtx_MINUS (Pmode,
634 XEXP (orig, 0),
635 pic_base));
ee890fe2
SS
636#if defined (TARGET_TOC) /* i.e., PowerPC */
637 /* Generating a new reg may expose opportunities for
638 common subexpression elimination. */
49bd1d27 639 rtx hi_sum_reg = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
6f94a68e
GK
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);
ee890fe2 647
6f94a68e
GK
648 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
649
542a8afa
RH
650 mem = gen_const_mem (GET_MODE (orig),
651 gen_rtx_LO_SUM (Pmode,
652 hi_sum_reg, offset));
6f94a68e
GK
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;
ee890fe2 658#else
6f94a68e
GK
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);
ee890fe2
SS
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
6f94a68e
GK
684 emit_insn (gen_rtx_USE (VOIDmode,
685 gen_rtx_REG (Pmode,
686 PIC_OFFSET_TABLE_REGNUM)));
ee890fe2
SS
687#endif
688
6f94a68e
GK
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)));
ee890fe2 695 }
9c808aad 696
ee890fe2 697#if !defined (TARGET_TOC)
ee890fe2 698 emit_move_insn (reg, pic_ref);
542a8afa 699 pic_ref = gen_const_mem (GET_MODE (orig), reg);
f9b0ac3b 700#endif
ee890fe2
SS
701 }
702 else
703 {
704
705#ifdef HAVE_lo_sum
9c808aad 706 if (GET_CODE (orig) == SYMBOL_REF
ee890fe2
SS
707 || GET_CODE (orig) == LABEL_REF)
708 {
6f94a68e
GK
709 rtx offset = gen_rtx_CONST (Pmode,
710 gen_rtx_MINUS (Pmode,
711 orig, pic_base));
ee890fe2
SS
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
49bd1d27 720 reg = gen_reg_rtx (Pmode);
ee890fe2 721 }
9c808aad 722
ee890fe2
SS
723 hi_sum_reg = reg;
724
6f94a68e
GK
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)));
ee890fe2
SS
735 pic_ref = reg;
736#else
6f94a68e
GK
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);
ee890fe2
SS
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
6f94a68e
GK
761 emit_insn (gen_rtx_USE (VOIDmode,
762 pic_offset_table_rtx));
ee890fe2 763#endif
6f94a68e
GK
764 pic_ref = gen_rtx_PLUS (Pmode,
765 pic,
766 gen_rtx_CONST (Pmode,
767 gen_rtx_MINUS (Pmode,
768 orig, pic_base)));
ee890fe2
SS
769 }
770 }
771 }
772
ee890fe2
SS
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)
9c808aad 800
ee890fe2
SS
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 {
ed8908e7 810 pic_ref = plus_constant (base, INTVAL (orig));
ee890fe2
SS
811 is_complex = 1;
812 }
813 else
6f94a68e 814 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
ee890fe2 815
ee890fe2
SS
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);
389fdba0 833 addr = replace_equiv_address (orig, addr);
ee890fe2
SS
834 emit_move_insn (reg, addr);
835 pic_ref = reg;
836 }
837
838 return pic_ref;
839}
840
11abc112
MM
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. */
ee890fe2 844
11abc112
MM
845static int
846machopic_output_indirection (void **slot, void *data)
ee890fe2 847{
11abc112
MM
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;
ee890fe2 856
11abc112
MM
857 symbol = p->symbol;
858 sym_name = XSTR (symbol, 0);
1adaa117 859 ptr_name = p->ptr_name;
11abc112
MM
860
861 if (p->stub_p)
ee890fe2 862 {
ee890fe2
SS
863 char *sym;
864 char *stub;
ee890fe2 865
ee890fe2
SS
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] == '+')
9c808aad 870 strcpy (sym, sym_name);
ee890fe2 871 else
789a4ea3 872 sprintf (sym, "%s%s", user_label_prefix, sym_name);
ee890fe2 873
11abc112
MM
874 stub = alloca (strlen (ptr_name) + 2);
875 if (ptr_name[0] == '*' || ptr_name[0] == '&')
876 strcpy (stub, ptr_name + 1);
ee890fe2 877 else
11abc112 878 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
ee890fe2 879
ca472546 880 machopic_output_stub (asm_out_file, sym, stub);
ee890fe2 881 }
699c914a 882 else if (! indirect_data (symbol)
156a126c
MS
883 && (machopic_symbol_defined_p (symbol)
884 || SYMBOL_REF_LOCAL_P (symbol)))
ee890fe2 885 {
11abc112
MM
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);
ee890fe2 892 }
11abc112
MM
893 else
894 {
699c914a
MS
895 rtx init = const0_rtx;
896
11abc112
MM
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
699c914a
MS
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),
11abc112
MM
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)
699c914a 927 htab_traverse_noresize (machopic_indirections,
11abc112
MM
928 machopic_output_indirection,
929 asm_out_file);
ee890fe2
SS
930}
931
9c808aad
AJ
932int
933machopic_operand_p (rtx op)
ee890fe2
SS
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)
11abc112 941 return machopic_symbol_defined_p (op);
ee890fe2
SS
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
11abc112
MM
952 && machopic_symbol_defined_p (XEXP (op, 0))
953 && machopic_symbol_defined_p (XEXP (op, 1)))
ee890fe2
SS
954 return 1;
955
ee890fe2
SS
956 return 0;
957}
df56a27f
SS
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
9c808aad 964darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
df56a27f 965{
353e51f8 966 rtx sym_ref;
df56a27f 967
e1a4211d
SS
968 /* Do the standard encoding things first. */
969 default_encode_section_info (decl, rtl, first);
970
11abc112
MM
971 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
972 return;
e1a4211d 973
11abc112
MM
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)
f1a66265 979 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
df56a27f
SS
980 && ((TREE_STATIC (decl)
981 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
bfbdca0b 982 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
abe72dd8 983 && DECL_INITIAL (decl) != error_mark_node)))
11abc112 984 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
699c914a
MS
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;
4e08ba6c 990}
2cc07db4 991
8e3e233b
DP
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
ae46c4e0 1000void
9c808aad
AJ
1001machopic_select_section (tree exp, int reloc,
1002 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
ae46c4e0 1003{
3d7964d5 1004 void (*base_function)(void);
f1a66265
GK
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];
3d7964d5 1018 else if (TREE_READONLY (exp) || TREE_CONSTANT (exp))
f1a66265 1019 base_function = base_funs[3][weak_p];
3d7964d5 1020 else
f1a66265 1021 base_function = base_funs[4][weak_p];
9c808aad 1022
3d7964d5 1023 if (TREE_CODE (exp) == STRING_CST
9c808aad 1024 && ((size_t) TREE_STRING_LENGTH (exp)
3521b33c 1025 == strlen (TREE_STRING_POINTER (exp)) + 1))
3d7964d5
GK
1026 cstring_section ();
1027 else if ((TREE_CODE (exp) == INTEGER_CST || TREE_CODE (exp) == REAL_CST)
1028 && flag_merge_constants)
ae46c4e0
RH
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
3d7964d5 1041 base_function ();
ae46c4e0
RH
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 ();
ae46c4e0 1055 else
3d7964d5 1056 base_function ();
ae46c4e0
RH
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 ();
264fa2db
ZL
1102 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1103 objc_image_info_section ();
ae46c4e0
RH
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 ();
ae46c4e0 1112 else
3d7964d5 1113 base_function ();
ae46c4e0 1114 }
f1a66265
GK
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 }
ae46c4e0 1137 else
3d7964d5 1138 base_function ();
ae46c4e0
RH
1139}
1140
b64a1b53 1141/* This can be called with address expressions as "rtx".
991b6592 1142 They must go in "const". */
b64a1b53
RH
1143
1144void
9c808aad
AJ
1145machopic_select_rtx_section (enum machine_mode mode, rtx x,
1146 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
b64a1b53
RH
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 ();
86b0a4f3 1154 else if (MACHOPIC_INDIRECT
2e53734e
GK
1155 && (GET_CODE (x) == SYMBOL_REF
1156 || GET_CODE (x) == CONST
1157 || GET_CODE (x) == LABEL_REF))
1158 const_data_section ();
b64a1b53
RH
1159 else
1160 const_section ();
1161}
1162
2cc07db4 1163void
9c808aad 1164machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
2cc07db4 1165{
ab82a49f 1166 if (MACHOPIC_INDIRECT)
2cc07db4
RH
1167 mod_init_section ();
1168 else
1169 constructor_section ();
c8af3574
RH
1170 assemble_align (POINTER_SIZE);
1171 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2cc07db4 1172
ab82a49f 1173 if (! MACHOPIC_INDIRECT)
2cc07db4
RH
1174 fprintf (asm_out_file, ".reference .constructors_used\n");
1175}
1176
1177void
9c808aad 1178machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
2cc07db4 1179{
ab82a49f 1180 if (MACHOPIC_INDIRECT)
2cc07db4
RH
1181 mod_term_section ();
1182 else
1183 destructor_section ();
c8af3574
RH
1184 assemble_align (POINTER_SIZE);
1185 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2cc07db4 1186
ab82a49f 1187 if (! MACHOPIC_INDIRECT)
2cc07db4
RH
1188 fprintf (asm_out_file, ".reference .destructors_used\n");
1189}
e2500fed 1190
5eb99654 1191void
9c808aad 1192darwin_globalize_label (FILE *stream, const char *name)
5eb99654
KG
1193{
1194 if (!!strncmp (name, "_OBJC_", 6))
1195 default_globalize_label (stream, name);
1196}
1197
4746cf84 1198void
c18a5b6c
MM
1199darwin_asm_named_section (const char *name,
1200 unsigned int flags ATTRIBUTE_UNUSED,
1201 tree decl ATTRIBUTE_UNUSED)
4746cf84 1202{
f1a66265 1203 fprintf (asm_out_file, "\t.section %s\n", name);
4746cf84
MA
1204}
1205
4746cf84 1206void
f1a66265 1207darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
4746cf84 1208{
f1a66265 1209 /* Darwin does not use unique sections. */
4746cf84
MA
1210}
1211
005c1a13
GK
1212/* Handle a "weak_import" attribute; arguments as in
1213 struct attribute_spec.handler. */
1214
1215tree
1216darwin_handle_weak_import_attribute (tree *node, tree name,
1217 tree ARG_UNUSED (args),
1218 int ARG_UNUSED (flags),
1219 bool * no_add_attrs)
1220{
1221 if (TREE_CODE (*node) != FUNCTION_DECL)
1222 {
1223 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1224 *no_add_attrs = true;
1225 }
1226 else
1227 declare_weak (*node);
1228
1229 return NULL_TREE;
1230}
45cc4783
MS
1231
1232static void
1233no_dead_strip (FILE *file, const char *lab)
1234{
005c1a13 1235 fprintf (file, ".no_dead_strip %s\n", lab);
45cc4783
MS
1236}
1237
4746cf84 1238/* Emit a label for an FDE, making it global and/or weak if appropriate.
eeab4d81
MS
1239 The third parameter is nonzero if this is for exception handling.
1240 The fourth parameter is nonzero if this is just a placeholder for an
4746cf84 1241 FDE that we are omitting. */
eeab4d81 1242
4746cf84 1243void
eeab4d81 1244darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
4746cf84
MA
1245{
1246 tree id = DECL_ASSEMBLER_NAME (decl)
1247 ? DECL_ASSEMBLER_NAME (decl)
1248 : DECL_NAME (decl);
1249
1250 const char *prefix = "_";
1251 const int prefix_len = 1;
1252
1253 const char *base = IDENTIFIER_POINTER (id);
1254 unsigned int base_len = IDENTIFIER_LENGTH (id);
1255
1256 const char *suffix = ".eh";
4746cf84
MA
1257
1258 int need_quotes = name_needs_quotes (base);
1259 int quotes_len = need_quotes ? 2 : 0;
eeab4d81
MS
1260 char *lab;
1261
1262 if (! for_eh)
1263 suffix = ".eh1";
4746cf84 1264
eeab4d81 1265 lab = xmalloc (prefix_len + base_len + strlen (suffix) + quotes_len + 1);
4746cf84
MA
1266 lab[0] = '\0';
1267
1268 if (need_quotes)
1269 strcat(lab, "\"");
1270 strcat(lab, prefix);
1271 strcat(lab, base);
1272 strcat(lab, suffix);
1273 if (need_quotes)
1274 strcat(lab, "\"");
1275
1276 if (TREE_PUBLIC (decl))
f1a66265 1277 fprintf (file, "\t%s %s\n",
4746cf84
MA
1278 (DECL_VISIBILITY (decl) != VISIBILITY_HIDDEN
1279 ? ".globl"
1280 : ".private_extern"),
1281 lab);
1282
f1a66265
GK
1283 if (DECL_WEAK (decl))
1284 fprintf (file, "\t.weak_definition %s\n", lab);
4746cf84
MA
1285
1286 if (empty)
45cc4783
MS
1287 {
1288 fprintf (file, "%s = 0\n", lab);
1289
1290 /* Mark the absolute .eh and .eh1 style labels as needed to
1291 ensure that we don't dead code strip them and keep such
1292 labels from another instantiation point until we can fix this
1293 properly with group comdat support. */
1294 no_dead_strip (file, lab);
1295 }
4746cf84
MA
1296 else
1297 fprintf (file, "%s:\n", lab);
1298
1299 free (lab);
1300}
1301
1302/* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
eeab4d81 1303
4746cf84
MA
1304void
1305darwin_non_lazy_pcrel (FILE *file, rtx addr)
1306{
4746cf84
MA
1307 const char *nlp_name;
1308
1309 if (GET_CODE (addr) != SYMBOL_REF)
1310 abort ();
1311
11abc112 1312 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
4746cf84
MA
1313 fputs ("\t.long\t", file);
1314 ASM_OUTPUT_LABELREF (file, nlp_name);
1315 fputs ("-.", file);
1316}
1317
6ce4806b
MA
1318/* Emit an assembler directive to set visibility for a symbol. The
1319 only supported visibilities are VISIBILITY_DEFAULT and
1320 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
1321 extern". There is no MACH-O equivalent of ELF's
1322 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
1323
1324void
1325darwin_assemble_visibility (tree decl, int vis)
1326{
1327 if (vis == VISIBILITY_DEFAULT)
1328 ;
1329 else if (vis == VISIBILITY_HIDDEN)
1330 {
1331 fputs ("\t.private_extern ", asm_out_file);
1332 assemble_name (asm_out_file,
1333 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
1334 fputs ("\n", asm_out_file);
1335 }
1336 else
1337 warning ("internal and protected visibility attributes not supported"
1338 "in this configuration; ignored");
1339}
1340
7606e68f
SS
1341/* Output a difference of two labels that will be an assembly time
1342 constant if the two labels are local. (.long lab1-lab2 will be
1343 very different if lab1 is at the boundary between two sections; it
1344 will be relocated according to the second section, not the first,
1345 so one ends up with a difference between labels in different
1346 sections, which is bad in the dwarf2 eh context for instance.) */
1347
1348static int darwin_dwarf_label_counter;
1349
1350void
9c808aad
AJ
1351darwin_asm_output_dwarf_delta (FILE *file, int size ATTRIBUTE_UNUSED,
1352 const char *lab1, const char *lab2)
7606e68f 1353{
4746cf84
MA
1354 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
1355 && lab2[0] == '*' && lab2[1] == 'L');
7606e68f
SS
1356
1357 if (islocaldiff)
1358 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
1359 else
1360 fprintf (file, "\t%s\t", ".long");
1361 assemble_name (file, lab1);
1362 fprintf (file, "-");
1363 assemble_name (file, lab2);
1364 if (islocaldiff)
1365 fprintf (file, "\n\t.long L$set$%d", darwin_dwarf_label_counter++);
1366}
1367
a5fe455b 1368void
9c808aad 1369darwin_file_end (void)
a5fe455b
ZW
1370{
1371 machopic_finish (asm_out_file);
1372 if (strcmp (lang_hooks.name, "GNU C++") == 0)
1373 {
1374 constructor_section ();
1375 destructor_section ();
1376 ASM_OUTPUT_ALIGN (asm_out_file, 1);
1377 }
92b9a671 1378 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
a5fe455b
ZW
1379}
1380
699c914a
MS
1381/* True, iff we're generating fast turn around debugging code. When
1382 true, we arrange for function prologues to start with 4 nops so
1383 that gdb may insert code to redirect them, and for data to accessed
1384 indirectly. The runtime uses this indirection to forward
1385 references for data to the original instance of that data. */
1386
1387int darwin_fix_and_continue;
1388const char *darwin_fix_and_continue_switch;
1389
e2500fed 1390#include "gt-darwin.h"